Given an array of integers, find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order.

**Examples**:

Input: arr[] = {1, 9, 3, 10, 4, 20, 2}Output: 4Explanation:The subsequence 1, 3, 4, 2 is the longest subsequence of consecutive elementsInput: arr[] = {36, 41, 56, 35, 44, 33, 34, 92, 43, 32, 42}Output: 5Explanation:The subsequence 36, 35, 33, 34, 32 is the longest subsequence of consecutive elements.

**Naive Approach:** The idea is to first sort the array and find the longest subarray with consecutive elements.

After sorting the array run a loop and keep a count and max (both initially zero). Run a loop from start to end and if the current element is not equal to the previous (element+1) then set the count to 1 else increase the count. Update max with a maximum of count and max.

`// C++ program to find longest ` `// contiguous subsequence ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns length of the longest ` `// contiguous subsequence ` `int` `findLongestConseqSubseq(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `ans = 0, count = 0; ` ` ` ` ` `// sort the array ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// find the maximum length ` ` ` `// by traversing the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// if the current element is equal ` ` ` `// to previous element +1 ` ` ` `if` `(i > 0 && arr[i] == arr[i - 1] + 1) ` ` ` `count++; ` ` ` `// reset the count ` ` ` `else` ` ` `count = 1; ` ` ` ` ` `// update the maximum ` ` ` `ans = max(ans, count); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 9, 3, 10, 4, 20, 2 }; ` ` ` `int` `n = ` `sizeof` `arr / ` `sizeof` `arr[0]; ` ` ` `cout << ` `"Length of the Longest contiguous subsequence is "` ` ` `<< findLongestConseqSubseq(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Length of the Longest contiguous subsequence is 4

**Complexity Analysis:**

**Time complexity:**O(nLogn).

Time to sort the array is O(nlogn).**Auxiliary space :**O(1).

As no extra space is needed.

*Thanks to Hao.W for suggesting the above solution.*

**Efficient solution:**

This problem can be solved in O(n) time using an **Efficient Solution**. The idea is to use Hashing. We first insert all elements in a Set. Then check all the possible starts of consecutive subsequences.

**Algorithm:**

- Create an empty hash.
- Insert all array elements to hash.
- Do following for every element arr[i]
- Check if this element is the starting point of a subsequence. To check this, simply look for arr[i] – 1 in the hash, if not found, then this is the first element a subsequence.
- If this element is the first element, then count the number of elements in the consecutive starting with this element. Iterate from arr[i] + 1 till the last element that can be found.
- If the count is more than the previous longest subsequence found, then update this.

Below image is a dry run of the above approach:

Below is the implementation of the above approach:

## C/C++

`// C++ program to find longest ` `// contiguous subsequence ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns length of the longest ` `// contiguous subsequence ` `int` `findLongestConseqSubseq(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `unordered_set<` `int` `> S; ` ` ` `int` `ans = 0; ` ` ` ` ` `// Hash all the array elements ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `S.insert(arr[i]); ` ` ` ` ` `// check each possible sequence from ` ` ` `// the start then update optimal length ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// if current element is the starting ` ` ` `// element of a sequence ` ` ` `if` `(S.find(arr[i] - 1) == S.end()) { ` ` ` `// Then check for next elements ` ` ` `// in the sequence ` ` ` `int` `j = arr[i]; ` ` ` `while` `(S.find(j) != S.end()) ` ` ` `j++; ` ` ` ` ` `// update optimal length if ` ` ` `// this length is more ` ` ` `ans = max(ans, j - arr[i]); ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 9, 3, 10, 4, 20, 2 }; ` ` ` `int` `n = ` `sizeof` `arr / ` `sizeof` `arr[0]; ` ` ` `cout << ` `"Length of the Longest contiguous subsequence is "` ` ` `<< findLongestConseqSubseq(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find longest ` `// consecutive subsequence ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `ArrayElements { ` ` ` `// Returns length of the longest ` ` ` `// consecutive subsequence ` ` ` `static` `int` `findLongestConseqSubseq(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `HashSet<Integer> S = ` `new` `HashSet<Integer>(); ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Hash all the array elements ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `S.add(arr[i]); ` ` ` ` ` `// check each possible sequence from the start ` ` ` `// then update optimal length ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) { ` ` ` `// if current element is the starting ` ` ` `// element of a sequence ` ` ` `if` `(!S.contains(arr[i] - ` `1` `)) { ` ` ` `// Then check for next elements ` ` ` `// in the sequence ` ` ` `int` `j = arr[i]; ` ` ` `while` `(S.contains(j)) ` ` ` `j++; ` ` ` ` ` `// update optimal length if this ` ` ` `// length is more ` ` ` `if` `(ans < j - arr[i]) ` ` ` `ans = j - arr[i]; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Testing program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `9` `, ` `3` `, ` `10` `, ` `4` `, ` `20` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println( ` ` ` `"Length of the Longest consecutive subsequence is "` ` ` `+ findLongestConseqSubseq(arr, n)); ` ` ` `} ` `} ` `// This code is contributed by Aakash Hasija ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find longest contiguous subsequence ` ` ` `from` `sets ` `import` `Set` `def` `findLongestConseqSubseq(arr, n): ` ` ` ` ` `s ` `=` `Set` `() ` ` ` `ans ` `=` `0` ` ` ` ` `# Hash all the array elements ` ` ` `for` `ele ` `in` `arr: ` ` ` `s.add(ele) ` ` ` ` ` `# check each possible sequence from the start ` ` ` `# then update optimal length ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# if current element is the starting ` ` ` `# element of a sequence ` ` ` `if` `(arr[i]` `-` `1` `) ` `not` `in` `s: ` ` ` ` ` `# Then check for next elements in the ` ` ` `# sequence ` ` ` `j ` `=` `arr[i] ` ` ` `while` `(j ` `in` `s): ` ` ` `j` `+` `=` `1` ` ` ` ` `# update optimal length if this length ` ` ` `# is more ` ` ` `ans ` `=` `max` `(ans, j` `-` `arr[i]) ` ` ` `return` `ans ` ` ` `# Driver function ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `7` ` ` `arr ` `=` `[` `1` `, ` `9` `, ` `3` `, ` `10` `, ` `4` `, ` `20` `, ` `2` `] ` ` ` `print` `"Length of the Longest contiguous subsequence is "` `, ` ` ` `print` `findLongestConseqSubseq(arr, n) ` ` ` `# Contributed by: Harshit Sidhwa ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` `using` `System.Collections.Generic; ` ` ` `// C# program to find longest consecutive subsequence ` ` ` `public` `class` `ArrayElements { ` ` ` `// Returns length of the longest consecutive subsequence ` ` ` `public` `static` `int` `findLongestConseqSubseq(` `int` `[] arr, ` `int` `n) ` ` ` `{ ` ` ` `HashSet<` `int` `> S = ` `new` `HashSet<` `int` `>(); ` ` ` `int` `ans = 0; ` ` ` ` ` `// Hash all the array elements ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `S.Add(arr[i]); ` ` ` `} ` ` ` ` ` `// check each possible sequence from the start ` ` ` `// then update optimal length ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `// if current element is the starting ` ` ` `// element of a sequence ` ` ` `if` `(!S.Contains(arr[i] - 1)) { ` ` ` `// Then check for next elements in the ` ` ` `// sequence ` ` ` `int` `j = arr[i]; ` ` ` `while` `(S.Contains(j)) { ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// update optimal length if this length ` ` ` `// is more ` ` ` `if` `(ans < j - arr[i]) { ` ` ` `ans = j - arr[i]; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Testing program ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `[] arr = ` `new` `int` `[] { 1, 9, 3, 10, 4, 20, 2 }; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(` `"Length of the Longest consecutive subsequence is "` `+ findLongestConseqSubseq(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

**Output:**

Length of the Longest contiguous subsequence is 4

**Complexity Analysis:**

**Time complexity:**O(n).

Only one traversal is needed and the time complexity is O(n) under the assumption that hash insert and search take O(1) time.**Auxiliary space :**O(n).

To store every element in hashmap O(n) space is needed.

*Thanks to Gaurav Ahirwar for the above solution.*

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Longest Increasing consecutive subsequence
- Printing longest Increasing consecutive subsequence
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Maximum subsequence sum such that no three are consecutive
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Largest increasing subsequence of consecutive integers
- Longest Zig-Zag Subsequence
- Longest subsequence with no 0 after 1
- Longest subsequence with a given AND value | O(N)
- Longest subsequence with a given AND value
- Longest Uncommon Subsequence
- Longest Bitonic Subsequence in O(n log n)
- Longest Increasing Subsequence using BIT
- Length of longest subsequence whose XOR value is odd
- Longest subsequence whose average is less than K
- Longest Repeating Subsequence
- Longest Bitonic Subsequence | DP-15
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence | DP-3
- Longest Common Subsequence | DP-4