Examples:
Input: {1, 2, 3, 4, 5, 6, 7, 9, 10, 111, 123} Output: 7 Input: {1, 2, 3, 4, 5, 6, 7, 8, 9, 111, 123} Output: 9
The key here is to traverse through the input array and find out number of consecutive integers.
1. Add all the integers from an input array to a HashSet
2. Loop
through the input array. For each element, calculate the number of consecutive right (integer + 1) or consecutive left (integer - 1) integers by checking their presence in the set created above.
3. At the end of the iteration, return the max count
.
public static int longestConsecutiveValuesLength(int[] array) { }
C
Java
Python