-
Notifications
You must be signed in to change notification settings - Fork 71
/
celebrityProblem.cpp
58 lines (52 loc) · 1.28 KB
/
celebrityProblem.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int celebrity(int M[][3], int n)
{
stack<int> st;
for(int i=0;i<n;i++){
st.push(i);
}
while(st.size()!=1){
int A = st.top();
st.pop();
int B = st.top();
st.pop();
if(M[A][B]==1){
st.push(B);
}
if(M[B][A]==1){
st.push(A);
}
}
int c = st.top();
bool rowCheck = false;
int zeroCount = 0;
for(int i=0;i<n;i++){
if(M[c][i]==0){
zeroCount++;
}
}
if(zeroCount==n){
rowCheck = true;
}
bool colCheck = false;
int oneCount = 0;
for(int i=0;i<n;i++){
if(M[i][c]==1){
oneCount++;
}
}
if(oneCount==n-1){
colCheck = true;
}
if(rowCheck == 1 && colCheck == 1){
return c;
}
return -1;
}
int main (){
int M[3][3] = { {0,1,0},{0,0,0},{0,1,0} };
int c = celebrity(M,3);
cout<<"Celebrity: "<<c;
}