-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCelebrityProblem.cpp
56 lines (44 loc) · 907 Bytes
/
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
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int getCelebrity(vector<vector<int>> arr)
{
stack<int> s;
int n = arr.size();
for (int i = 0; i < n; i++)
{
s.push(i); // push all index value
}
while (s.size() > 1)
{
int i = s.top();
s.pop();
int j = s.top();
s.pop();
if (arr[i][j] == 0)
{
s.push(i);
}
else
{
s.push(j);
}
}
int celeb = s.top();
for (int i = 0; i < n; i++)
{
if ((i != celeb) && (arr[i][celeb] == 0 || arr[celeb][i] == 1))
{
return -1; // no celebrity exits
}
}
return celeb;
}
int main()
{
vector<vector<int>> arr = {{0, 1, 0}, {0, 0, 0}, {0, 1, 0}};
int ans = getCelebrity(arr);
cout << "Celebrity is : " << ans << endl;
return 0;
}