-
Notifications
You must be signed in to change notification settings - Fork 112
/
Solution.java
37 lines (28 loc) · 852 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
/**
* @author Oleg Cherednik
* @since 11.02.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(findLongestConsecutiveElementsLength(100, 4, 200, 1, 3, 2)); // 4
}
public static int findLongestConsecutiveElementsLength(int... arr) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int num : arr) {
min = Math.min(min, num);
max = Math.max(max, num);
}
boolean[] existed = new boolean[max - min + 1];
for (int num : arr)
existed[num - min] = true;
int res = 0;
for (int i = 0, cur = 0; i < existed.length; i++) {
if (existed[i])
res = Math.max(res, ++cur);
else
cur = 0;
}
return res;
}
}