-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCelebrityProblem1.cpp
47 lines (38 loc) · 1.07 KB
/
CelebrityProblem1.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
// https://www.geeksforgeeks.org/the-celebrity-problem/
#include <vector>
#include <iostream>
bool knows(std::vector<std::vector<int>>& matrix, int i, int j)
{
return matrix[i][j];
}
int findCelebrity(std::vector<std::vector<int>>& matrix)
{
int celebrityId = -1;
int n = matrix.size();
std::vector<int> indegree(n, 0);
std::vector<int> outdegree(n, 0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (knows(matrix, i, j)) {
outdegree[i]++;
indegree[j]++;
}
}
}
for (int i = 0; i < n; ++i) {
if (indegree[i] == n - 1 && outdegree[i] == 0) {
return i;
}
}
return celebrityId;
}
int main()
{
std::vector<std::vector<int>> matrix {{0, 0, 1, 0},
{0, 0, 1, 0},
{0, 0, 0, 0},
{0, 0, 1, 0}};
int id = findCelebrity(matrix);
std::cout << "Celebrity index : " << id << "\n";
return 0;
}