Longest Consecutive Sequence

Hero Image

Problem Overview

The problem "Longest Consecutive Sequence" is a classical coding challenge often encountered in technical interviews and competitive programming. It requires devising an efficient algorithm to find the length of the longest consecutive elements sequence in an unsorted array of integers.

The challenge can be accessed on LeetCode: Longest Consecutive Sequence.

Understanding the Problem

Given an unsorted array of integers, the task is to determine the length of the longest sequence of consecutive numbers. Importantly, the integers within the array may appear in any order and the sequence does not need to be contiguous within the array.

For example:

  • Input: nums = [100, 4, 200, 1, 3, 2]
  • Output: 4 (The longest consecutive sequence is [1, 2, 3, 4])

Key DSA Concepts and Theory

To solve this problem efficiently, one must understand key concepts related to hash sets and arrays.

HashSet

HashSet is a part of Java's collections framework and provides a structure similar to a set in mathematics. Its core property is that it does not allow duplicate elements. It has average time complexity of O(1) for operations like add, remove, and contains.

Key Operations:

  • Add: Insert an element.
  • Contains: Check if an element exists.
  • Remove: Delete an element.

Arrays

An array is a data structure that stores a collection of items at contiguous memory locations. Arrays provide constant time access to elements using an index but modifying them may take linear time in the worst case.

Solution Approach

To solve the problem efficiently, we use a HashSet to track the numbers and determine the longest sequence by finding the start of a sequence and extending it. Here's a step-by-step breakdown:

  1. Initialize a HashSet: Insert all elements from the array into a HashSet to remove duplicates and allow O(1) access to elements.

  2. Iterate through the Array: For each element, check if it is potentially the start of a sequence by seeing if there's no previous consecutive number in the set.

  3. Find Sequence Length: If current element is a sequence start, proceed to find the sequence length by incrementing the number and checking for existence in the set.

  4. Track the Maximum Length: Use a variable to keep track of the maximum length of all sequences found.

Here's the implementation in C++ and Java:

C++ Code

#include <unordered_set>
#include <vector>

int longestConsecutive(std::vector<int>& nums) {
    if (nums.empty()) return 0;

    std::unordered_set<int> num_set(nums.begin(), nums.end());
    int longest_streak = 0;

    for (int num : num_set) {
        if (num_set.find(num - 1) == num_set.end()) {  // If it is the start of a sequence
            int current_num = num;
            int current_streak = 1;

            while (num_set.find(current_num + 1) != num_set.end()) {
                current_num += 1;
                current_streak += 1;
            }

            longest_streak = std::max(longest_streak, current_streak);
        }
    }

    return longest_streak;
}

Java Code

import java.util.HashSet;
import java.util.Set;

public class Solution {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) return 0;

        Set<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }

        int longestStreak = 0;

        for (int num : numSet) {
            if (!numSet.contains(num - 1)) {  // If it is the start of a sequence
                int currentNum = num;
                int currentStreak = 1;

                while (numSet.contains(currentNum + 1)) {
                    currentNum += 1;
                    currentStreak += 1;
                }

                longestStreak = Math.max(longestStreak, currentStreak);
            }
        }

        return longestStreak;
    }
}

Time and Space Complexity Analysis

  • Time Complexity: O(n). Although there are nested loops, each element is considered only once due to set operations.
  • Space Complexity: O(n), where n is the number of elements in the array, since we store all elements in a set.

Common Mistakes to Avoid

  1. Assuming Sorted Input: Always remember the array is unsorted.
  2. Not Handling Duplicates: Make sure to use a data structure that ignores duplicates like a HashSet.
  3. Mistaking Sequence Requirement: The consecutive numbers need not be in sequential array indices, just values.

Similar Problems on LeetCode

Additional Resources and References

This article should provide a deep understanding of the "Longest Consecutive Sequence" problem, its solution approach, and related concepts. Armed with this knowledge, solving related problems should be more intuitive and streamlined.