-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (32 loc) · 910 Bytes
/
Solution.java
File metadata and controls
35 lines (32 loc) · 910 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Result {
public static int pickingNumbers(List<Integer> a) {
Collections.sort(a);
int max = 0;
int i = 0;
int j = 1;
while (i < a.size() && j < a.size() && i < j) {
if (Math.abs(a.get(i) - a.get(j)) <= 1) {
max = Math.max(max, ++j - i);
} else {
if (++i == j) j++;
}
}
return max;
}
}
public class Solution {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = scan.nextInt();
List<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++) {
a.add(scan.nextInt());
}
System.out.println(Result.pickingNumbers(a));
scan.close();
}
}