30. The Celebrity Problem
β GFG solution to the Celebrity Problem: find the celebrity in a party using optimal linear elimination technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
A celebrity is a person who is known to all but does not know anyone at a party. A party is being organized by some people. A square matrix mat[][]
of size n*n
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. Your task is to return the index of the celebrity in the party, if the celebrity does not exist, return -1.
Note: Follow 0-based indexing.
π Examples
Example 1
Input: mat[][] = [[1, 1, 0],
[0, 1, 0],
[0, 1, 1]]
Output: 1
Explanation: 0th and 2nd person both know 1st person and 1st person does not know anyone.
Therefore, 1 is the celebrity person.
Example 2
Input: mat[][] = [[1, 1],
[1, 1]]
Output: -1
Explanation: Since both the people at the party know each other. Hence none of them is a celebrity person.
Example 3
Input: mat[][] = [[1]]
Output: 0
Explanation: Single person is considered a celebrity as they satisfy the condition trivially.
π Constraints
$1 \le \text{mat.size()} \le 1000$
$0 \le \text{mat}[i][j] \le 1$
$\text{mat}[i][i] = 1$
β
My Approach
The optimal approach uses Linear Elimination technique to find the celebrity candidate in O(n) time:
Linear Elimination Algorithm
Find Celebrity Candidate:
Start with candidate
c = 0
.For each person
i
from 1 to n-1:If current candidate
c
knows personi
(mat[c][i] == 1), thenc
cannot be celebrity.Update candidate to
c = i
.
After this loop, we have a potential celebrity candidate.
Verify Celebrity:
Check if the candidate satisfies both conditions:
Condition 1: Celebrity should not know anyone (except themselves)
Condition 2: Everyone should know the celebrity
If any condition fails, return -1.
Key Insight:
In each iteration, we eliminate at least one person from being a celebrity.
If
mat[c][i] == 1
, thenc
cannot be celebrity (knows someone).If
mat[c][i] == 0
, theni
cannot be celebrity (not known by potential celebrity).
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of people. We make two passes through the matrix: first to find candidate (O(n)) and second to verify (O(n)).
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables regardless of input size.
π§ Code (C)
int celebrity(int **m, int n) {
int c = 0;
for (int i = 1; i < n; i++) if (m[c][i]) c = i;
for (int i = 0; i < n; i++) if (i != c && (m[c][i] || !m[i][c])) return -1;
return c;
}
π§βπ» Code (C++)
class Solution {
public:
int celebrity(vector<vector<int>>& m) {
int n = m.size(), c = 0;
for (int i = 1; i < n; i++) if (m[c][i]) c = i;
for (int i = 0; i < n; i++) if (i != c && (m[c][i] || !m[i][c])) return -1;
return c;
}
};
β Code (Java)
class Solution {
public int celebrity(int m[][]) {
int n = m.length, c = 0;
for (int i = 1; i < n; i++) if (m[c][i] == 1) c = i;
for (int i = 0; i < n; i++) if (i != c && (m[c][i] == 1 || m[i][c] == 0)) return -1;
return c;
}
}
π Code (Python)
class Solution:
def celebrity(self, m):
n, c = len(m), 0
for i in range(1, n):
if m[c][i]: c = i
for i in range(n):
if i != c and (m[c][i] or not m[i][c]): return -1
return c
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated