-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpyDetected.java
49 lines (37 loc) · 1.13 KB
/
SpyDetected.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
import java.util.Scanner;
public class SpyDetected {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for ( int i = 0; i < t; i++) {
int n = sc.nextInt();
int[] arr = new int[n];
for ( int j = 0; j < n; j++) {
arr[j] = sc.nextInt();
}
int spyIdx = 0;
int spyNeighbourIdx = 0;
boolean spy = true;
for (int j = 0; j < n-1; j++) {
if ( arr[j] != arr[j+1]) {
spyIdx = j;
spyNeighbourIdx = j+1;
break;
}
}
for ( int j = 0; j < n; j++) {
if ( arr[j] == arr[spyIdx] && j != spyIdx) {
spy = false;
break;
}
}
if (!spy) {
System.out.println(spyNeighbourIdx+1);
}
else {
System.out.println(spyIdx+1);
}
}
sc.close();
}
}