-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThe Celebrity Problem.cpp
39 lines (36 loc) · 1.17 KB
/
The Celebrity Problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Problem Link : https://practice.geeksforgeeks.org/problems/the-celebrity-problem/1#
int celebrity(vector<vector<int> >& M, int n)
{
stack<int> st;
// Adding all person in the stack
for(int i = 0; i < n; i++){
st.push(i);
}
while(st.size() != 1){
// i will take 2 persons from top
int a = st.top();
st.pop();
int b = st.top();
st.pop();
// if 'a' knows 'b' then 'a' can't be celebrity so i will add 'b' in the stack again
// and if 'a' doesn't know 'b' then 'b' can't be celebrity so i will add 'a' in the stack again
if(M[a][b] == 1){
st.push(b);
}else{
st.push(a);
}
}
int a = st.top();
st.pop();
// i will check that last element is celebrity or not
// if not then i will return -1
for(int i = 0; i < n; i++){
if(M[a][i] == 1){
return -1;
}
if(M[i][a] == 0 && i != a){
return -1;
}
}
return a;
}