VOOZH about

URL: https://www.geeksforgeeks.org/dsa/the-celebrity-problem/

⇱ The Celebrity Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

The Celebrity Problem

Last Updated : 25 Aug, 2025

Given a square matrix mat[][] of size n x n, where mat[i][j] == 1 means person i knows person j, and mat[i][j] == 0 means person i does not know person j, find the celebrity person where,

A celebrity is defined as someone who:

  • Is known by everyone else
  • Does not know anyone (except themselves)

Return the index of the celebrity if one exists, otherwise return -1.

Note: It is guaranteed that mat[i][i] == 1 for all i

Examples:

Input: mat[][] = [[1, 1, 0],
[0, 1, 0],
[0, 1, 1]]
Output: 1
Explanation: 0th and 2nd person both know 1. Therefore, 1 is the celebrity.

Input: mat[][] = [[1, 1],
[1, 1]]
Output: -1
Explanation: The two people at the party both know each other. None of them is a celebrity.

Input: mat[][] = [[1]]
Output: 0

[Naive Approach] Using Adjacency List - O(n2) Time and O(n) Space

The idea is to model the problem as a directed graph where edges represent “knows” relationships.
A celebrity, if present, will be the single vertex with indegree = n and outdegree = 1, meaning they are known by everyone else but does not know anyone (except themselves).

Step by Step Implementation:

  • Create two arrays indegree and outdegree, to store the indegree and outdegree
  • Run a nested loop, the outer loop from 0 to n and inner loop from 0 to n.
  • For every pair i, j check if i knows j then increase the outdegree of i and indegree of j.
  • For every pair i, j check if j knows i then increase the outdegree of j and indegree of i.
  • Run a loop from 0 to n and find the id where the indegree is n and outdegree is 1.

Output
1

[Better Approach] Using Stack - O(n) Time and O(n) Space

The idea is to use a stack to eliminate non-celebrities by comparing pairs. If one person knows the other, the first is discarded; otherwise, the second is discarded. Repeat until one candidate remains, then verify if they meet the celebrity conditions.

Step by Step Implementation:

  • Create a stack and push all the ids in the stack.
  • Run a loop while there are more than 1 element in the stack.
  • Pop the top two elements from the stack (represent them as A and B)
  • If A knows B, then A can't be a celebrity and push B in the stack. Else if A doesn't know B, then B can't be a celebrity push A in the stack.
  • Assign the remaining element in the stack as the celebrity.
  • Run a loop from 0 to n-1 and find the count of persons who knows the celebrity and the number of people whom the celebrity knows.
    => If the count of persons who knows the celebrity is n-1 and the count of people whom the celebrity knows is 0 then return the id of the celebrity else return -1.

Output
1

[Expected Approach] Using Two Pointers - O(n) Time and O(1) Space

The idea is to use two pointers, one from start and one from the end. Assume the start person is A, and the end person is B. If A knows B, then A must not be the celebrity. Else, B must not be the celebrity. At the end of the loop, only one index will be left as a celebrity. Go through each person again and check whether this is the celebrity. 

Step by Step Implementation:

  • Create two indices i and j, where i = 0 and j = n-1
  • Run a loop until i is less than j.
  • Check if i knows j, then i can't be a celebrity. so increment i, i.e. i++
  • Else j cannot be a celebrity, so decrement j, i.e. j--
  • Assign i as the celebrity candidate
  • Now at last check whether the candidate is actually a celebrity by re-running a loop from 0 to n-1  and constantly checking if the candidate knows a person or if there is a candidate who does not know the candidate. Then we should return -1. else at the end of the loop, we can be sure that the candidate is actually a celebrity.

Output
1
Comment