-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSuccessorPairs.java
More file actions
40 lines (34 loc) · 1003 Bytes
/
CountSuccessorPairs.java
File metadata and controls
40 lines (34 loc) · 1003 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
import java.util.*;
public class CountSuccessorPairs {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int temp = 0;
int result = 0;
int counter = 0;
for (int i = 0; i < size; i++) {
if (i == 0) {
temp = arr[i];
result = 1;
} else {
if (temp == arr[i]) {
result++;
} else if (temp + 1 == arr[i]) {
counter += result;
temp = arr[i];
result = 1;
} else {
result = 1;
temp = arr[i];
}
}
}
System.out.println(counter);
sc.close();
}
}