-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsecutivePairs.java
More file actions
41 lines (37 loc) · 978 Bytes
/
Copy pathConsecutivePairs.java
File metadata and controls
41 lines (37 loc) · 978 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
class ConsecutivePairs {
public static void main(String[] args) {
int[] array = {1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4};
boolean result = isConsecutivePairs(array);
System.out.println(result);
}
public static boolean isConsecutivePairs(int[] array) {
if (array.length < 6) return false;
int i = 0;
int j = 1;
int counter = 0;
while (i < array.length && j < array.length) {
if (array[i] != array[j]) {
if (counter > 0 && array[i] == array[i-1] && array[j] - array[i] == 1) {
counter = counter;
} else {
counter = 0;
}
i++;
j++;
} else {
if (counter > 0 && array[i] == array[i-1]) {
i++;
j++;
} else if (counter > 0 && array[i] - array[i-1] != 1) {
counter = 0;
} else {
i += 2;
j += 2;
counter++;
}
}
if (counter == 3) return true;
}
return false;
}
}