The Celebrity Problem | Practice | GeeksforGeeks

A celebrity is a person who is known to all but does not know anyone at a party. If you go to a party of N people, find if there is a celebrity in the party or not.
A square NxN matrix M[][] is used to represent people at the party such that if an element of row i and column j  is set to 1 it means ith person knows jth person. Here M[i][i] will always be 0.
Note: Follow 0 based indexing.
 
Example 1:
Input:
N = 3
M[][] = {{0 1 0},
         {0 0 0}, 
         {0 1 0}}
Output: 1
Explanation: 0th and 2nd person both
know 1. Therefore, 1 is the celebrity. 

Example 2:
Input:
N = 2
M[][] = {{0 1},
         {1 0}}
Output: -1
Explanation: The two people at the party both
know each other. None of them is a celebrity.

Your Task:
You don't need to read input or print anything. Complete the function celebrity() which takes the matrix M and its size N as input parameters and returns the index of the celebrity. If no such celebrity is present, return -1.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
2 <= N <= 3000
0 <= M[][] <= 1
0
raghavgaur502 days ago
//Easy C++ solution with O(1) space and O(N) time complexity.
  int celebrity(vector<vector<int> >& M, int n)    {       // code here        int result=0;int flag=0;       for(int i=0;i<M.size();i++)       {           for(int j=0;j<M[0].size();j++)           {               if(M[i][j]==1 )               {                   if(result==i)                   result++;                   flag=1;               }                          }       }      if(flag==0)      return -1;       if(result==n)       return -1;       return result;   }
0
aiswaryajayaraman2 days ago
Simple C++ Solution
int celebrity(vector<vector<int> >& M, int n)    {       int row=0;       int col=0;       int celebrity_arr[n]={0};       int known[n]={0};       for(row=0;row<n;row++)       {           for(col=0;col<n;col++)           {               if(M[row][col]==1)               {                   if(M[col][row]==0)                   celebrity_arr[col]+=1;                                      known[row]=1;               }           }       }       for(int i=0;i<n;i++)       {           if(celebrity_arr[i]>=1 && known[i]==0 )           return i;       }       return -1;   }
0
guptamansi78674 days ago
class Solution {   public:   //Function to find if there is a celebrity in the party or not.   int celebrity(vector<vector<int> >& m, int n)    {       // push all index in stack       stack<int>s;       for(int i=0;i<n;i++){           s.push(i);                  }       // while one element is not left in stack till then top two element        while(s.size()>1){           int a =s.top();           s.pop();           int b = s.top();           s.pop();           // if a knows b then push b           if(m[a][b]==1){               s.push(b);                          }           else{               s.push(a);           }                  }       // top the last element present in stack and consider it potential candidate       int ans=  s.top();       // verify whether its celebrity or not       // count row of potential candidate if all row zero it can  be celebrity       int r=0;       for(int i=0;i<n;i++){           if(m[ans][i]==0){               r++;           }       }       // if all row not zero it cant be celebrity       if(r!= n){           return -1;       }       // if all column will zero then only celebrity       int c=0;       for(int i=0;i<n;i++){           if(m[i][ans]==1){               c++;           }       }       if(c !=n-1){           return -1;       }       // if all row and all column of potential candidate is zero return that candidate       return ans;       // code here    }};
0
ns2847194 days ago
  bool knows(vector<vector<int> >&m ,int a,int b){if(m[a][b] == 1){ return true;}elsereturn false;}
      int celebrity(vector<vector<int> >& M, int n)    {       // code here        stack<int> s;      for(int i=0;i<n;i++)     {     s.push(i);      }
     while(s.size() > 1)    {   int a = s.top();   s.pop();   int b = s.top();   s.pop();   if(knows(M,a,b))   {     s.push(b);     }    else   {   s.push(a);    } }
int ans = s.top();
int zerocount=0;for(int i=0;i<n;i++){if(M[ans][i]==0)zerocount++;}
if(zerocount!=n) return -1; int onecount=0;for(int i=0;i<n;i++){if(M[i][ans]==1)onecount++;}
if(onecount!=n-1)return -1;
return ans;   }
0
suryarajsabi6 days ago
Java solution
 
int celebrity(int M[][], int n)   {    // code here     int c=0, tc=0, index=-1;        for(int i=0; i<n; i++){        c=0;        for(int j=0; j<n; j++){                        if(M[i][j] == 0){               c++;                // if all are 0               tc++;            }        }                if(c==n){            index = i;         }    }    if(tc==n*n && c==n) return -1;       return index;       }
0
manish14091 week ago
easily undestandle code in c++
class Solution {   private:   bool knows(vector<vector<int> >& M,int row,int col){       if(M[row][col])return true;       else return false;   }   bool celCheck(vector<vector<int> >& M,int cel,int n){       int zerocnt=0,onecnt=0;       for(int i=0;i<n;i++){           if(M[cel][i]==0)zerocnt++;           if(M[i][cel])onecnt++;       }       if(zerocnt==n and onecnt==n-1)return true;       else return false;   }   public:      int celebrity(vector<vector<int> >& M, int n)    {       /manish kumar//       stack<int>st;       //push all person in stack       for(int i=0;i<n;i++)st.push(i);       //doing small check for two person       while(st.size()>1){           int a=st.top();st.pop();           int b=st.top();st.pop();           if(knows(M,a,b))st.push(b);           else st.push(a);       }       int cel=st.top();//may be celebrity       if(celCheck(M,cel,n))return cel;//celCheck verify that        else return -1;   }};
0
shreshthchaturvedi24012 weeks ago
class Solution {   public:   //Function to find if there is a celebrity in the party or not.      int celebrity(vector<vector<int> >& M, int n)       {              stack<int> s;       for(int i = 0; i < n;i++)       {           s.push(i);       }              while(s.size() > 1)       {           int a = s.top();           s.pop();                      int b = s.top();           s.pop();                      if(M[a][b] == 1)           {               // a knows b               s.push(b); // possible celebrity           }           else{                              // a do not knows b               s.push(a); // possible celebrity           }       }              int ans = s.top();              //verify row :              int countZero = 0;              for(int i = 0; i < n; i++)       {            if(M[ans][i] == 0)            {                countZero++;            }                   }              if(countZero != n)       return -1;                    // verify column :              int countOne = 0;               for(int i = 0; i < n; i++)       {                       if(M[i][ans] == 1)            {                countOne++;            }                   }              if(countOne!=n-1 )       return -1;              return ans;          }};
+1
farooqmohammed3462 weeks ago
//basic brute force solution with T.C O(N^2)
class Solution {   public:   //Function to find if there is a celebrity in the party or not.   int celebrity(vector<vector<int> >& arr, int n)    {       // code here       int celeb=-1;      for(int i=0;i<n;i++)      {          int couclassnt=0;          int count1=0;          for(int j=0;j<n;j++)          {             if(arr[i][j]==0)             {                 count++;             }          }          for(int j=0;j<n;j++)          {              if(arr[j][i]==1)              {                  count1++;              }          }          if(count==n&&count1==n-1)          {              celeb=i;          }      }
       return celeb;   }};
0
stoicamm952 weeks ago
C++, Total time taken 0.57/1.73
 
    int celebrity(vector<vector<int> >& M, int n)    {       for(int i=0; i<n; i++){           bool isCelebrity = true;           for(int j=0; j<n; j++){               if(M[i][j] != 0){ // not a celebrity                   isCelebrity = false;                   break;               }           }                      if(isCelebrity){               bool allKnowsHim = true;               for(int j=0; j<n; j++){                   if(i == j)                       continue;                   if(M[j][i] != 1){                       allKnowsHim = false;                   }               }                              if(allKnowsHim)                   return i;           }       }              return -1;   }
0
ankitap99322 weeks ago
class Solution 
{
    public:
    bool knows(vector<vector<int>> &M,int n,int a,int b){
        if(M[a][b]==1)
            return true;
        else 
            return false;
    }
    
    //Function to find if there is a celebrity in the party or not.
    int celebrity(vector<vector<int> >& M, int n) {
        // code here 
        //step-1 add all elements in stack
        stack<int> stk;
        for(int i=0;i<n;i++){
            stk.push(i);
        }
        
        //step-2 check for two elements if they know eachother
        while(stk.size()>1){
            int a=stk.top();
            stk.pop();
            int b=stk.top();
            stk.pop();
            
            if(knows(M,n,a,b)){// check if a knows b
                stk.push(b);    //potential candidate for celebrity
            }
            else{
                stk.push(a);
            }
        }
        int celebrity=stk.top();
        
        //step-3 if no celebrity then return -1
        for(int i=0;i<n;i++){
            if(i!=celebrity){
                if(M[celebrity][i]==1 || M[i][celebrity]==0){
                    return -1;
                }
            }
        }
        
    return celebrity;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.