The idea is as follows:

first, if person A knows person B, then B could be the candidate of being a celebrity, A must not be a celebrity. We iterate all the n persons and we will have a candidate that everyone knows this candidate.

second, we check two things after we get this candidate. 1. If this candidate knows other person in the group, if the candidate knows anyone in the group, then the candidate is not celebrity, return -1; 2. if everyone knows this candidate, if anyone does not know the candidate, return -1;

// Forward declaration of the knows API.

bool knows(int a, int b);

class Solution {

public:

```
int findCelebrity(int n) {
if(n<=1) return n;
int candidate = 0;
for(int i=1; i<n; i++){
if ( !knows(i,candidate) ){
candidate = i;
}
}
for(int j=0; j<n; j++){
if(j== candidate) continue;
if( !knows(j,candidate) || knows(candidate,j) ){
//if j does not know candidate, or candidate knows j, return -1;
return -1;
}
}
return candidate;
}
```

};