public int longestConsecutive(int[] nums) {
Map<Integer, Boolean> numMap = new ConcurrentHashMap<>();
Map<Integer, Integer> maxMap = new ConcurrentHashMap<>();
for (int i : nums) {
numMap.put(i, false);
}
int max = 0;
for (int n : numMap.keySet()) {
numMap.remove(n);
if (maxMap.containsKey(n - 1)) {
maxMap.put(n, maxMap.get(n - 1) + 1);
max = Math.max(maxMap.get(n), max);
continue;
}
int lessThan = 0;
while (numMap.containsKey(n - lessThan - 1)) {
numMap.remove(n - 1);
lessThan++;
}
maxMap.put(n, lessThan + 1);
if (lessThan + 1 > max) {
max = lessThan + 1;
}
}
return max;
}
This is my solution to Longest Consecutive Sequence problem and it works for all but 1 of the 70 cases. The 70th case array has length 100,000. I checked my solution for this and other lengths of the array and I observed that the max value returned is always 65537 for arrays of size greater than 65537. I can't seem to figure out why that is the case. I wonder if it has something to do with using ConcurrentHashmap.
Here's my test:
@Test
public void test() {
for (int i = 0; i < 100000; i++) {
int n = i;
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = j;
}
assertEquals(n, longestConsecutive(arr));
}
}
The test fails at 65538 by returning 65537. And I also checked for some random values greater than that and it failed for them too with the same value.