-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNOFIX.java
More file actions
52 lines (44 loc) · 1.3 KB
/
Copy pathNOFIX.java
File metadata and controls
52 lines (44 loc) · 1.3 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
import java.util.ArrayList;
import java.util.Scanner;
public class NOFIX {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Test cases
int T = scanner.nextShort();
for (int i = 1; i <= T; i++) {
// Input
int N = scanner.nextInt();
ArrayList<Long> A = new ArrayList<>();
for (int j = 0; j < N; j++) {
A.add(j, scanner.nextLong());
}
// Logic
int count = 0;
int temp = isContainFixedPoint(A, N);
if (temp != -1) {
int result = -1;
long K = Long.MAX_VALUE;
while (result == -1) {
A.add(temp, K);
N++;
temp = isContainFixedPoint(A, N);
if (temp == -1) {
result = 1;
}
count++;
K--;
}
}
// Output
System.out.println(count);
}
}
public static int isContainFixedPoint(ArrayList<Long> A, int N) {
for (int i = 0; i < N; i++) {
if (A.get(i) == (i + 1)) {
return i;
}
}
return -1;
}
}