-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram109.java
More file actions
98 lines (79 loc) · 2.81 KB
/
Program109.java
File metadata and controls
98 lines (79 loc) · 2.81 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
The Celebrity Problem is a famous stack-based interview problem.
Here's a complete explanation, approach using stack, Java code with comments, and complexity analysis — all placement-ready.
--> Problem Statement:
In a party of n people, a celebrity is defined as someone who:
- Is known by everyone else, but
- Knows no one (not even themselves)
You're given a matrix M[][] of size n x n:
- M[i][j] == 1 → Person i knows person j
- M[i][j] == 0 → Person i does not know person j
Return the index of the celebrity, or -1 if there is no celebrity.
--> Approach using Stack:
-> Key Observations:
- If A knows B, then A cannot be a celebrity.
- If A doesn't know B, then B cannot be a celebrity.
-> Stack-Based Algorithm:
1. Push all n people into a stack.
2. Pop two people A and B.
3. If A knows B, A is not a celebrity; push B back.
4. If A doesn’t know B, B is not a celebrity; push A back.
5. Repeat until 1 candidate remains.
6. Verify that this person is a celebrity:
- They don’t know anyone.
- Everyone knows them.
*/
import java.util.Stack;
public class Program109 {
// Sample matrix M[i][j] = 1 if person i knows person j
static int[][] M = {
{0, 1, 1},
{0, 0, 1},
{0, 0, 0}
};
// Returns true if person a knows person b
static boolean knows(int a, int b) {
return M[a][b] == 1;
}
// Function to find the celebrity
static int findCelebrity(int n) {
Stack<Integer> stack = new Stack<>();
// Step 1: Push everyone to stack
for (int i = 0; i < n; i++) {
stack.push(i);
}
// Step 2: Eliminate non-celebrities
while (stack.size() > 1) {
int a = stack.pop();
int b = stack.pop();
// If a knows b, then a is not a celebrity
if (knows(a, b)) {
stack.push(b);
} else {
// If a doesn't know b, then b is not a celebrity
stack.push(a);
}
}
// Step 3: Potential celebrity
int candidate = stack.pop();
// Step 4: Final verification
for (int i = 0; i < n; i++) {
if (i != candidate) {
// candidate should not know i, and everyone should know candidate
if (knows(candidate, i) || !knows(i, candidate)) {
return -1; // Not a celebrity
}
}
}
return candidate;
}
public static void main(String[] args) {
int n = M.length;
int result = findCelebrity(n);
if (result == -1) {
System.out.println("No celebrity found");
} else {
System.out.println("Celebrity is person: " + result);
}
}
}