Techie Delight
Coding made easy
Given an array of integers, find the length of longest subsequence formed by the consecutive integers. The subsequence should contain all distinct values and the character set should be consecutive, irrespective of its order.

The problem differs from finding largest sub-array formed by the consecutive integers. Unlike subarrays, subsequences are not required to occupy consecutive positions within the original sequences.

For example, consider array { 2, 0, 6, 1, 5, 3, 7 }. The largest subsequence formed by the consecutive integers is { 2, 0, 1, 3 }.

Naive solution is to sort the array in ascending order and compare the consecutive elements to find the maximum length sub-array with consecutive integers. The time complexity of this solution would be O(nlog(n)).

We can do better using Hashing. The idea is to consider each element of the input sequence and find the maximum length consecutive subsequence starting with it. i.e. for every element e, we check for presence of elements e+1, e+2, e+3.. in the input. We can optimize the code by using set for constant time lookups to determine if the element present in the input sequence or not.
The algorithm can be implemented as below in C++, Java and Python:

C++

Download   Run Code

Output:

The length of maximum length consecutive subsequence is 4

Java

Download   Run Code

Output:

The length of maximum length consecutive subsequence is 4

Python

Download   Run Code

Output:

The length of maximum length consecutive subsequence is 4

Download   Run Code
Output:

The length of maximum length consecutive subsequence is 4

Download   Run Code
Output:

The length of maximum length consecutive subsequence is 4

Download   Run Code
Output:

The length of maximum length consecutive subsequence is 4

The time complexity of above solution is O(n) and auxiliary space used by the program is O(n).

Exercise: Extend the solution to print the maximum length consecutive subsequence.
I think your optimized solution has worse runtime complexity than the simply sorting the list. in case a list already in sorted order, your code will require 1 + 2 + 3 + 4 + ….. +n lookups, which is (n2), which is a lot worse than n(logn)
Note that we’re calling the while loop only when the current element is candidate for starting of a sequence. The time complexity remains O(n).
Good point. Thanks. Love the content.
The ‘Hashing’ solution is incorrect, since there is no sense of ‘consecutive’.
Try:
“vector vec = { 2, 4, 0, 6, 1, 5, 3, 7 };”
It’s correct. The length of maximum length consecutive subsequence is 8 which is formed by {2, 4, 0, 6, 1, 5, 3, 7} whose character set [0-7] is consecutive.
0,1,2,3,4,5,6,7 is not a *subsequence* of the original sequence.
No, the subsequence is {2, 4, 0, 6, 1, 5, 3, 7} whose digits are consecutive “irrespective” of their order.
Ah I see, I missed the ‘irrespective of order’ part. Thanks.
What about elements are in decreasing order…I think it will be n2.
The ascending or descending order doesn’t matter. A set is constructed out of the input elements.
Enter your email address to subscribe to new posts and receive notifications of new posts by email.

source