Javatpoint Logo
Javatpoint Logo

Longest Consecutive Sequence

In this tutorial, we will write the Python program to find the longest consecutive sequence. It is a commonly asked programming question in the technical interview.

A Longest Consecutive Sequence problem typically involves finding the longest sequence of consecutive numbers within an array or list of integers. The idea is to determine the length of the longest consecutive sequence of numbers without gaps.

Example 1:

Solutions

We will solve this problem using the various approach -

Naïve Approach

The approach involves sorting the array, removing duplicates, and then iterating through it. During the iteration, we maintain two counters: `count` and `max`, both initially set to zero. Within the loop, if the current element is not one greater than the previous one, we reset `count` to one; otherwise, we increment it. At each step, we update `max` with the maximum value between `count` and the current `max`.

Let's understand the following example -

Example -

Output:

The length of the longest consecutive subsequence in input_arr1 is: 4
The length of the longest consecutive subsequence in input_arr2 is: 5

Explanation -

We sort the list of numbers to make it easier to identify consecutive elements.

We initialize count to 1 to keep track of the current consecutive sequence length and max_count to 1 to store the maximum consecutive sequence length.

We iterate through the sorted list of numbers and check if the current element is not equal to the previous element plus one. If they are not consecutive, we reset count to 1. Otherwise, we increment count. We update max_count with the maximum value between its current value and count.

Finally, we return max_count, which represents the length of the longest consecutive subsequence in the input list.

Time Complexity - O(n log n), where n is the number of elements in the input list.

Auxiliary space: O(N). Extra space is needed for storing distinct elements.

Longest Consecutive Subsequence using sorting without removing duplicate elements

The approach consists of first sorting the array. Subsequently, we count the consecutive elements within the sorted array, excluding any repeated elements. Let's understand the following example -

Example -

Output:

The length of the longest consecutive subsequence in input_arr1 is: 4
The length of the longest consecutive subsequence in input_arr2 is: 5

Explanation -

The code starts by sorting the input list of numbers to make it easier to identify consecutive elements. Two variables, `longest_sequence` and `current_sequence`, are initialized to 1. These variables keep track of the length of the longest consecutive subsequence found so far and the length of the current consecutive subsequence being examined, respectively.

Then iterates through the sorted list and checks for consecutive elements. If the current element is exactly one greater than the previous element, it increments the `current_sequence` because it indicates a consecutive element. If the current element is different from the previous one, implying a non-duplicate, the `current_sequence` is reset to 1 to start counting a new potential consecutive subsequence.

After each iteration, the code updates `longest_sequence` with the maximum value between its current value and `current_sequence`. This ensures that it keeps track of the longest consecutive subsequence found so far.

Finally, the function returns the value of `longest_sequence`, representing the length of the longest consecutive subsequence in the input list.

Longest Consecutive Subsequence using Hashing

To find the longest consecutive subsequence using hashing, you can follow a more efficient approach that doesn't involve sorting. Here's a Python program using hashing to achieve this.

Example -

Output:

The length of the longest consecutive subsequence in input_arr1 is: 4
The length of the longest consecutive subsequence in input_arr2 is: 5

Explanation -

We start by converting the input list nums into a set called num_set for faster lookup.

We initialize longest_sequence to 0, which will store the length of the longest consecutive subsequence.

We iterate through the elements in num_set and for each element, we check if num - 1 is not in num_set. If it's not present, it means num is the start of a potential consecutive sequence.

Inside the loop, we initialize current_num with the current element num and current_sequence to 1. Then, we use a while loop to check for consecutive numbers in the positive direction (i.e., current_num + 1) and increment current_sequence accordingly.

We update longest_sequence with the maximum value between its current value and current_sequence. This ensures that we keep track of the longest consecutive subsequence found so far.

Finally, we return the value of longest_sequence, which represents the length of the longest consecutive subsequence in the input list nums.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news