-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindCelebs.java
More file actions
49 lines (31 loc) · 801 Bytes
/
Copy pathfindCelebs.java
File metadata and controls
49 lines (31 loc) · 801 Bytes
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
package stacks;
import java.util.Stack;
public class findCelebs {
public static int celebrity(int[][] arr, int n) {
Stack<Integer> stack=new Stack<>();
for(int i=0;i<n;i++){
stack.push(i);
}
while(stack.size()>1){
int a=stack.peek();
stack.pop();
int b=stack.peek();
stack.pop();
if(arr[a][b]==1){
stack.push(b);
}
else{
// stack.pop();
stack.push(a);
}
}
int celeb=stack.peek();
stack.pop();
for(int i=0;i<n;i++){
if(i!=celeb && (arr[celeb][i]==1 || arr[i][celeb]==0)){
return -1;
}
}
return celeb;
}
}