# Longest Consecutive Sequence

Posted: 28 Sep, 2020

Difficulty: Moderate

#### 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?
```

##### 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
```

Approach 1

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.

Approach 2

- The idea is to sort the array, then iterate through the array and find the longest subarray containing consecutive elements.
- We first initialize the variable ‘COUNT’
**= 0**which stores the length of the consecutive sequence and ‘MX’**= 0**which stores the longest length of consecutive subsequence. - Now run a loop and check if ‘ARR[i - 1]’ + 1 equal to ‘ARR[i]’ then it will include in the current consecutive subsequence by increment ‘COUNT’
- If ‘ARR[i - 1]’ is equal to ‘ARR[i]’ then it means it shouldn’t be considered in consecutive sequence because the consecutive sequence is of the form ['NUM', 'NUM' + 1, 'NUM' + 2,...,'NUM' + L].
- Else If ‘ARR[i - 1]’ + 1 is not equal to ‘ARR[i]’ then we set to ‘COUNT’ to 1. For finding the longest length we update ‘MX’

Approach 3

We can improve our time complexity of searching the next consecutive element in the array by using a Hash Table which can check the presence of an element in O(1).

The steps are as follows:

- Store all the elements in the Hash table first.
- For every element check, if it is a starting element of sequence or not. by simply checking if ‘ARR[i]’ - 1 is present in a hashtable or not. If it is present so it means ‘ARR[i]’ can’t be the first element of the sequence.
- If the element is the first element then count the total number of elements that can occur in sequence by incrementing the element by 1 in each iteration of the while loop.
- If the count is more than the longest consecutive sequence then we update it.