forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelebrityProblem.java
More file actions
61 lines (51 loc) · 1.76 KB
/
CelebrityProblem.java
File metadata and controls
61 lines (51 loc) · 1.76 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
57
58
59
60
61
package com.geeksforgeeks.stack;
import java.util.Stack;
public class CelebrityProblem {
private static boolean[][] connections = {
{false, false, true, false},
{false, false, true, false},
{false, false, false, false},
{false, false, true, false}};
private static boolean knows(int A, int B) {
return connections[A][B];
}
/**
* 1) Push All guest in the stack
* 2) Pop 2 guest at a time and check whether A knows B
* 3) if Yes then throw A and push B back else push A and throw B
* 4) Repeat 2-3 till there is only 1 entry in the stack
* 5) Check the Acquaintance of this guest with others if no acquaintances found, Voila this is our celebrity.
*
* @param guestsInTheParty
*/
public static void findCelebrity(int guestsInTheParty) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < guestsInTheParty; i++) {
stack.push(i);
}
while (stack.size() > 1) {
int A = stack.pop();
int B = stack.pop();
if (knows(A, B)) {
stack.push(B);
} else {
stack.push(A);
}
}
int celebrity = stack.pop();
boolean celebrityFound = true;
for (int i = 0; i < guestsInTheParty; i++) {
if (i != celebrity) {
if (knows(celebrity, i)) {
celebrityFound = false;
break;
}
}
}
System.out.println(celebrityFound ? "Celebrity is " + (celebrity + 1) : "Celebrity not present in the party");
}
public static void main(String[] args) {
int guestsInTheParty = 4;
findCelebrity(guestsInTheParty);
}
}