-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.java
41 lines (29 loc) · 1009 Bytes
/
Solution.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
import java.util.stream.IntStream;
public class Solution {
public static boolean solve(int[] n) {
int foundProblemAt = -1;
for (int i = 0; i < n.length - 1; i++) {
if (n[i] < n[i + 1])
continue;
if (foundProblemAt != -1)
return false;
foundProblemAt = i;
}
if (foundProblemAt == -1 || foundProblemAt == 0)
return true;
return n[foundProblemAt + 1] - n[foundProblemAt - 1] > 1;
}
public static void main(String[] args) {
// 1,2,3,8,10 true
// 1,2,9,8,10 true
// 10,5,7 true
// 10,5,1 false
// 1,11,9,8,10 false
// 11,2,3,8,10 true
// 1,2,3,3,10 false
int[][] tests = { { 1, 2, 3, 8, 10 }, { 1, 2, 9, 8, 10 }, { 10, 5, 7 }, { 10, 5, 1 }, { 1, 11, 9, 8, 10 },
{ 11, 2, 3, 8, 10 }, { 1, 2, 3, 3, 10 } };
for (int[] test : tests)
System.out.println(solve(test));
}
}