-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCelebrityProblem.java
70 lines (59 loc) · 1.95 KB
/
CelebrityProblem.java
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
/*
Name: Celebrity Problem
Source: PepCoding
Link: https://www.pepcoding.com/resources/online-java-foundation/stacks-and-queues/celebrity-problem-official/ojquestion
Statement: You are given a number n, representing the number of people in a party. You are given n strings of n length containing 0's and 1's
If there is a '1' in ith row, jth spot, then person i knows about person j.A celebrity is defined as somebody who knows no other person than himself but everybody
else knows him. If there is a celebrity print it's index otherwise print "none"
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class CelebrityProblem {
public static void main(String[] args) throws Exception {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[n][n];
for (int j = 0; j < n; j++) {
String line = br.readLine();
for (int k = 0; k < n; k++) {
arr[j][k] = line.charAt(k) - '0';
}
}
findCelebrity(arr);
}
public static void findCelebrity(int[][] arr) {
Stack<Integer> stk = new Stack<>();
for(int i=0; i<arr.length; i++)
{
stk.push(i);
}
while(stk.size()>=2)
{
int i = stk.pop();
int j = stk.pop();
if(arr[i][j]==1)
{
stk.push(j);
}
else
{
stk.push(i);
}
}
int cel = stk.pop();
for(int i=0; i<arr.length; i++)
{
if(i!=cel)
{
if(arr[cel][i]==1 || arr[i][cel]==0)
{
System.out.println("none");
return;
}
}
}
System.out.println(cel);
}
}