-
Notifications
You must be signed in to change notification settings - Fork 6
/
celebrity_problem.cpp
61 lines (50 loc) · 1.12 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// { Driver Code Starts
//Initial template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
bool knows(vector<vector<int>> &M, int a, int b) {
return M[a][b];
}
// Function to find if there is a celebrity in the party or not.
int celebrity(vector<vector<int> >& M, int n)
{
// code here
int a = 0, b = n - 1;
while(a < b) {
if(knows(M, a, b)) a++;
else b--;
}
for (int i = 0; i < n; i++) {
if ( (i != a) && (knows(M, a, i) || !knows(M, i, a)) )
return -1;
}
return a;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<vector<int> > M( n , vector<int> (n, 0));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>M[i][j];
}
}
Solution ob;
cout<<ob.celebrity(M,n)<<endl;
}
}
// } Driver Code Ends