Longest Consecutive Sequence in O(n log n) Time Using Python


Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Example 1:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: Longest consecutive sequence is [1, 2, 3, 4].
             Therefore its length is 4.

Example 2:

Input: [0, 2, 0, 1, 2, 3, 1]
Output: 4
Explanation: Longest consecutive sequence is [0, 1, 2, 3].
             Therefore its length is 4.
	     Note that we count each value once, even tho values 0, 1 and 2 appear 2 times each in nums

Note:

For this lesson, your algorithm should run in O(n log n) time and use O(1) extra space.
(There are faster solutions which we will discuss in future lessons)


Problem Definition

The problem requires finding the length of the longest consecutive sequence in an unsorted array of integers.

Understanding the Problem

The core challenge is to identify the longest sequence of consecutive integers in an unsorted array. This problem is significant in various applications such as data analysis, where identifying trends or patterns in data is crucial. A common pitfall is to assume that sorting the array and then finding the sequence is the only way, but there are more efficient methods.

Approach

To solve this problem, we can start with a naive approach and then optimize it:

Naive Solution

The naive solution involves sorting the array and then finding the longest consecutive sequence. This approach is straightforward but not optimal in terms of time complexity.

Optimized Solution

We can optimize the solution by using a set to keep track of the elements and then iterate through the array to find the longest sequence. This approach ensures that we only traverse the array once, achieving O(n) time complexity.

Algorithm

Here is a step-by-step breakdown of the optimized algorithm:

  1. Sort the array.
  2. Initialize variables to keep track of the longest sequence and the current sequence length.
  3. Iterate through the sorted array and check if the current element is consecutive to the previous element.
  4. If it is consecutive, increment the current sequence length.
  5. If it is not consecutive, update the longest sequence length if the current sequence is longer, and reset the current sequence length.
  6. Return the longest sequence length.

Code Implementation

def longest_consecutive(nums):
    # Sort the array
    nums.sort()
    
    # Initialize variables
    longest_streak = 0
    current_streak = 1
    
    # Iterate through the sorted array
    for i in range(1, len(nums)):
        # Check if the current element is consecutive to the previous element
        if nums[i] != nums[i - 1]:  # Skip duplicates
            if nums[i] == nums[i - 1] + 1:
                current_streak += 1
            else:
                longest_streak = max(longest_streak, current_streak)
                current_streak = 1
    
    # Return the longest sequence length
    return max(longest_streak, current_streak)

# Example usage
print(longest_consecutive([100, 4, 200, 1, 3, 2]))  # Output: 4
print(longest_consecutive([0, 2, 0, 1, 2, 3, 1]))  # Output: 4

Complexity Analysis

The time complexity of the optimized solution is O(n log n) due to the sorting step. The space complexity is O(1) as we are not using any extra space apart from a few variables.

Edge Cases

Potential edge cases include:

Testing

To test the solution comprehensively, consider the following test cases:

Thinking and Problem-Solving Tips

When approaching such problems, it is essential to:

Conclusion

In this blog post, we discussed how to find the longest consecutive sequence in an unsorted array of integers. We explored a naive solution and then optimized it to achieve O(n log n) time complexity. Understanding and solving such problems is crucial for developing problem-solving skills and improving algorithmic thinking.

Additional Resources

For further reading and practice, consider the following resources: