Skip to content

Commit 7816506

Browse files
Create celebrity_problem.cpp
1 parent 03a29d2 commit 7816506

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

celebrity_problem.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// { Driver Code Starts
2+
//Initial template for C++
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
// } Driver Code Ends
8+
//User function template for C++
9+
10+
class Solution
11+
{
12+
public:
13+
bool knows(vector<vector<int>> &M, int a, int b) {
14+
return M[a][b];
15+
}
16+
17+
// Function to find if there is a celebrity in the party or not.
18+
int celebrity(vector<vector<int> >& M, int n)
19+
{
20+
// code here
21+
22+
int a = 0, b = n - 1;
23+
24+
while(a < b) {
25+
if(knows(M, a, b)) a++;
26+
else b--;
27+
}
28+
29+
for (int i = 0; i < n; i++) {
30+
if ( (i != a) && (knows(M, a, i) || !knows(M, i, a)) )
31+
return -1;
32+
}
33+
34+
return a;
35+
}
36+
};
37+
38+
// { Driver Code Starts.
39+
40+
int main()
41+
{
42+
int t;
43+
cin>>t;
44+
while(t--)
45+
{
46+
int n;
47+
cin>>n;
48+
vector<vector<int> > M( n , vector<int> (n, 0));
49+
for(int i=0;i<n;i++)
50+
{
51+
for(int j=0;j<n;j++)
52+
{
53+
cin>>M[i][j];
54+
}
55+
}
56+
Solution ob;
57+
cout<<ob.celebrity(M,n)<<endl;
58+
59+
}
60+
}
61+
// } Driver Code Ends

0 commit comments

Comments
 (0)