Problem
Section titled “Problem”There are N people at a party. Each person has been assigned a unique id between 0 and N - 1 (both inclusive). A celebrity is a person who is known to everyone but does not know anyone at the party.
Given a helper function knows(A, B), it returns true if the person having id A knows the person having id B in the party, false otherwise. Your task is to find out the celebrity at the party. Return the id of the celebrity; if there is no celebrity at the party, return -1.
Note:
- The helper function
knowsis already implemented for you. knows(A, B)returnsfalseifAdoesn’t knowB.- You should not implement the helper function
knows, or speculate about its implementation. - You should minimize the number of calls to the function
knows(A, B). - There are at least 2 people at the party.
- At most one celebrity will exist.
Complete the function findCelebrity(int n).
Example 1:
Input: N = 2 knows(0, 1) returns false knows(1, 0) returns trueOutput: 0Explanation: Person 0 does not know person 1, and person 1 knows person 0. So person 0 is known to everyone but knows no one: 0 is the celebrity.Example 2:
Input: N = 2 knows(0, 1) returns true knows(1, 0) returns trueOutput: -1Explanation: Both people know each other, so there is no celebrity.Example 3:
Input: N = 2 knows(0, 1) returns false knows(1, 0) returns falseOutput: -1Explanation: Neither person knows the other, so neither is known to everyone: there is no celebrity.Constraints:
- Time limit: 1 sec
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Checking every person against every other costs calls. The key observation is that one call to knows(a, b) always rules out one of the two people:
- if
aknowsb, thenaknows someone, soais not the celebrity; - if
adoes not knowb, thenbis not known by everyone, sobis not the celebrity.
So we can run an elimination tournament: keep a single candidate, compare it with each next person, and let the loser drop out. After n - 1 calls, only one person can still be the celebrity. They are not guaranteed to be one (maybe nobody is), so we verify them.
Verification can reuse what the elimination already proved. The final candidate became the candidate at step candidate and then “beat” every later person i, which means knows(candidate, i) was false for all i > candidate. So only knows(candidate, i) for i < candidate and knows(i, candidate) for every i still need checking.
Algorithm
Section titled “Algorithm”- Eliminate.
candidate = 0. Forifrom1ton - 1: ifknows(candidate, i), setcandidate = i. - Verify. For every
i != candidate:- if
i < candidateandknows(candidate, i), return-1(the candidate knows someone); - if
!knows(i, candidate), return-1(someone does not know the candidate).
- if
- Return
candidate.
Walkthrough
Section titled “Walkthrough”Five people, where everyone knows person 2 and person 2 knows no one. Blue is the person compared with the current candidate (orange); grey people have been eliminated:
Complexity
Section titled “Complexity”- Time: — at most calls to
knows, each assumed . - Space: .
Edge cases
Section titled “Edge cases”- Nobody is a celebrity (Examples 2 and 3): some person still survives the elimination, and the verification rejects them with
-1. - Celebrity is person 0: the candidate never changes, the first verification check is skipped (no
i < 0), and onlyknows(i, 0)is verified.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
int findCelebrity(int n) { // Each call eliminates one person: whoever knows someone, or whoever is not known. int candidate = 0; for (int i = 1; i < n; i++) { if (knows(candidate, i)) candidate = i; }
// knows(candidate, i) is already known to be false for every i > candidate. for (int i = 0; i < n; i++) { if (i == candidate) continue; if (i < candidate && knows(candidate, i)) return -1; if (!knows(i, candidate)) return -1; } return candidate;}public class Solution { // The judge provides the helper as Runner.knows(A, B). public static int findCelebrity(int n) { // Each call eliminates one person: whoever knows someone, or whoever is not known. int candidate = 0; for (int i = 1; i < n; i++) { if (Runner.knows(candidate, i)) candidate = i; }
// knows(candidate, i) is already known to be false for every i > candidate. for (int i = 0; i < n; i++) { if (i == candidate) continue; if (i < candidate && Runner.knows(candidate, i)) return -1; if (!Runner.knows(i, candidate)) return -1; } return candidate; }}def findCelebrity(n, knows): # Each call eliminates one person: whoever knows someone, or whoever is not known. candidate = 0 for i in range(1, n): if knows(candidate, i): candidate = i
# knows(candidate, i) is already known to be false for every i > candidate. for i in range(n): if i == candidate: continue if i < candidate and knows(candidate, i): return -1 if not knows(i, candidate): return -1 return candidateGive it a try first
Attempt the problem on Coding Ninjas — the code will still be here.






