Start learning free

LESSON 265 OF 410 · HASH TABLE

Longest Consecutive Sequence II in Python (O(n) Time Complexity)

Practice O(n) Read the full explanation

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 though values 0, 1 and 2 appear 2 times each in nums

Note:

Your algorithm should run in O(n) time and use O(n) extra space.


STEP 1 OF 8
    Ctrl + Enter to check

    PYTHON · HASH TABLE

    Longest Consecutive Sequence in Python, explained

    Problem Definition

    The problem requires finding the length of the longest consecutive elements sequence in an unsorted array of integers. The algorithm should run in O(n) time and use O(n) extra space.

    Input:

    • An unsorted array of integers.

    Output:

    • An integer representing the length of the longest consecutive elements sequence.

    Constraints and Assumptions:

    • The array can contain both positive and negative integers.
    • Duplicate elements should be counted only once in the sequence.

    Example:

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

    Understanding the Problem

    The core challenge is to identify the longest sequence of consecutive integers in an unsorted array efficiently. This problem is significant in various applications such as data analysis, where finding patterns in data is crucial. A common pitfall is to sort the array first, which would result in a time complexity of O(n log n), not meeting the O(n) requirement.

    Approach

    To solve this problem efficiently, we can use a set to store the elements of the array. This allows O(1) average time complexity for lookups. The idea is to iterate through the array and for each element, check if it is the start of a sequence (i.e., the previous element is not in the set). If it is, we then count the length of the sequence by checking consecutive elements.

    Naive Solution:

    A naive solution would involve sorting the array and then finding the longest consecutive sequence. However, this approach has a time complexity of O(n log n) due to the sorting step, which is not optimal.

    Optimized Solution:

    The optimized solution involves using a set to achieve O(n) time complexity. Here’s the thought process:

    • Create a set of the array elements for O(1) lookups.
    • Iterate through the array, and for each element, check if it is the start of a sequence.
    • If it is the start, count the length of the sequence by checking consecutive elements in the set.
    • Keep track of the maximum sequence length found.

    Algorithm

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

    1. Create a set of the array elements.
    2. Initialize a variable to keep track of the maximum sequence length.
    3. Iterate through the array:
      • For each element, check if it is the start of a sequence (i.e., the previous element is not in the set).
      • If it is the start, initialize a counter and increment it while the next consecutive element is in the set.
      • Update the maximum sequence length if the current sequence is longer.

    Code Implementation

    def longest_consecutive(nums):
        # Create a set of the array elements
        num_set = set(nums)
        max_length = 0
    
        # Iterate through the array
        for num in nums:
            # Check if it is the start of a sequence
            if num - 1 not in num_set:
                current_num = num
                current_length = 1
    
                # Count the length of the sequence
                while current_num + 1 in num_set:
                    current_num += 1
                    current_length += 1
    
                # Update the maximum sequence length
                max_length = max(max_length, current_length)
    
        return max_length
    
    # 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 this approach is O(n) because each element is processed at most twice (once when checking if it is the start of a sequence and once when counting the length of the sequence). The space complexity is O(n) due to the set used to store the elements.

    Edge Cases

    Potential edge cases include:

    • An empty array: The output should be 0.
    • An array with one element: The output should be 1.
    • Arrays with all elements the same: The output should be 1.

    Examples:

    Input: []
    Output: 0
    
    Input: [1]
    Output: 1
    
    Input: [2, 2, 2]
    Output: 1

    Testing

    To test the solution comprehensively, consider a variety of test cases:

    • Simple cases with small arrays.
    • Cases with negative and positive integers.
    • Cases with duplicate elements.
    • Edge cases as discussed above.

    Example test cases:

    assert longest_consecutive([100, 4, 200, 1, 3, 2]) == 4
    assert longest_consecutive([0, 2, 0, 1, 2, 3, 1]) == 4
    assert longest_consecutive([]) == 0
    assert longest_consecutive([1]) == 1
    assert longest_consecutive([2, 2, 2]) == 1
    assert longest_consecutive([-1, -2, -3, -4, -5]) == 5
    

    Thinking and Problem-Solving Tips

    When approaching such problems:

    • Understand the problem requirements and constraints thoroughly.
    • Consider different approaches and their time complexities.
    • Use data structures that provide efficient operations for the task at hand (e.g., sets for O(1) lookups).
    • Think about edge cases and how your solution handles them.

    To improve problem-solving skills, practice similar problems, study algorithms, and understand their trade-offs.

    Conclusion

    In this blog post, we discussed how to find the length of the longest consecutive sequence in an unsorted array of integers efficiently. We explored the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free