Longest Consecutive Sequence

Moderate
0/80
Average time to solve is 40m
199 upvotes
Asked in companies
AmazonGoogleApple

Problem statement

You are given an unsorted array/list 'ARR' of 'N' integers. Your task is to return the length of the longest consecutive sequence.

The consecutive sequence is in the form ['NUM', 'NUM' + 1, 'NUM' + 2, ..., 'NUM' + L] where 'NUM' is the starting integer of the sequence and 'L' + 1 is the length of the sequence.

Note:

If there are any duplicates in the given array we will count only one of them in the consecutive sequence.
For example-
For the given 'ARR' [9,5,4,9,10,10,6].

Output = 3
The longest consecutive sequence is [4,5,6].
Follow Up:
Can you solve this in O(N) time and O(N) space complexity?
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line of input contains a single integer 'T', representing the number of test cases or queries to be run. Then the 'T' test cases follow.

The first line of each test case contains integer 'N' denoting the size of the array.

The second line of each test case contains 'N' single space-separated integers, elements of the array.  
Output format :
For each test case, print an integer in a single line that represents the length of the longest consecutive sequence.
Note :
You are not required to print the expected output; it has already been taken care of. Just implement the function. 
Constraints :
1 <= T <= 10
1 <= N <= 10^5
-10^9 <= ARR[i] <= 10^9

Time Limit: 1 sec
Sample Input 1 :
1 
5
33 20 34 30 35
Sample Output 1 :
3
Explanation to Sample Input 1 :
The longest consecutive sequence is [33, 34, 35].
Sample Input 2 :
1
7
1 9 3 10 4 20 2    
Sample Output 2 :
4
Explanation to Sample Input 2 :
The consecutive sequence is in the form ['NUM', 'NUM' + 1, 'NUM' + 2,...,'NUM' + 'L']. So in the given array, the longest consecutive sequence is [1,2,3,4] where 'NUM' = 1 and 'L' = 3. And the length of the sequence will be 'L' + 1 = 4.
Hint

Think of finding the next consecutive element in an array.

Approaches (3)
Brute Force

As we only need the consecutive elements in the form ['NUM', 'NUM' + 1, 'NUM' + 2,...,'NUM' + 'L']. The brute force approach is to traverse each element in the array ('NUM' = ‘ARR[i]’) and then keep finding ('NUM' + 1) in the array until we don't find the next consecutive element in the array.

 

Keep a track of the current length of the sequence. If the current length of the consecutive subsequence is greater than the longest length of consecutive subsequence then update it.

Time Complexity

O(N^3), where ‘N’ is the size of the array.

 

In the worst case, The outer loop will take O(N) time to traverse through the array, and then for each element, the inner while loop can also take up to O(N) time for selecting the next consecutive element.

 

And to check each next consecutive element is present in the array or not takes again O(N). So there are 3 nested loops therefore, the overall time complexity will be O(N^3).

Space Complexity

O(1)

 

Constant extra space is required.

Code Solution
(100% EXP penalty)
Longest Consecutive Sequence
Full screen
Console