forked from itsyadavRajkumar/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelebrityProblem.cpp
More file actions
56 lines (44 loc) · 1.16 KB
/
CelebrityProblem.cpp
File metadata and controls
56 lines (44 loc) · 1.16 KB
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 <bits/stdc++.h>
#include <list>
using namespace std;
// Max # of persons in the party
#define N 4
bool MATRIX[N][N] = { { 0, 0, 1, 0 },{ 0, 0, 1, 0 },{ 0, 0, 0, 0 },{ 0, 0, 1, 0 } };
bool knows(int A, int B)
{
return MATRIX[A][B];
}
int findCelebrity(int n) {
int celebrity = -1;
// Check one by one whether the person is a celebrity or not.
for(int i = 0; i < n; i++) {
bool knowAny = false, knownToAll = true;
// Check whether person with id 'i' knows any other person.
for(int j = 0; j < n; j++) {
if(knows(i, j)) {
knowAny = true;
break;
}
}
// Check whether person with id 'i' is known to all the other person.
for(int j = 0; j < n; j++) {
if(i != j and !knows(j, i)) {
knownToAll = false;
break;
}
}
if(!knowAny && knownToAll) {
celebrity = i;
break;
}
}
return celebrity;
}
// Driver code
int main()
{
int n = 4;
int id = findCelebrity(n);
id == -1 ? cout << "No celebrity" : cout << "Celebrity ID " << id;
return 0;
}