DIY: Longest Consecutive Sequence

Solve the interview question "Longest Consecutive Sequence" in this lesson.

Problem statement

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Input

The input will be an array of integers. The following is an example input:

[9,1,4,7,3,-1,0,5,8,-1,6]

Output

The output will be a single integer representing the length of the largest consecutive sequence in the input array. The following is an example output of the above input:

7

There are 7 consecutive integers in the input array, [3, 4, 5, 6, 7, 8, 9].

Coding exercise

You need to implement the LCS(arr) function, where arr is the array of unsorted integers. The function will return a single integer; this will be the length of the largest consecutive sequence present in arr.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.