Skip to main content

Longest Consecutive Sequence in an Array

Problem Statement:​

You are given an array of ā€˜N’ integers. You need to find the length of the longest sequence which contains the consecutive elements

  • Example:

    Example 1:

    Input: [100, 200, 1, 3, 2, 4]

    Output: 4

    Explanation: The longest consecutive subsequence is 1, 2, 3, and 4.

    Input: [3, 8, 5, 7, 6]

    Output: 4

    Explanation: The longest consecutive subsequence is 5, 6, 7, and 8.



āœ… Solution: HashSet + Sequence Starter Check​

int longestConsecutive(vector<int>& numbers) {
unordered_set<int> numberSet;
int maxLength = 0;

// Insert all elements into an unordered set
for (int num : numbers) {
numberSet.insert(num);
}

// Check each number: is it the start of a sequence?
for (int num : numberSet) {
if (numberSet.find(num - 1) == numberSet.end()) {
int currentNum = num;
int currentStreak = 1;

// Count length of the current sequence
while (numberSet.find(currentNum + 1) != numberSet.end()) {
currentNum++;
currentStreak++;
}

maxLength = max(maxLength, currentStreak);
}
}

return maxLength;
}


šŸ“ How It Works​

  • First, insert all elements into an unordered set to allow O(1) lookups.
  • Iterate over each number and check if it is the start of a sequence by checking if num - 1 is not in the set.
  • If it's a starting point, increment currentNum until the sequence breaks, counting the streak.
  • Track the maximum length found.

🧩 Key Logic​

If (num - 1) not in set → it's a sequence start
Then check for (num + 1), (num + 2), ... and count streak


ā±ļø Time & Space Complexity​

MetricValue
ā± TimeO(n)
šŸ—‚ SpaceO(n)

Each number is processed at most once due to set-based sequence checking.


āš ļø Edge Cases​

  • Empty array → return 0
  • All numbers same → return 1
  • Single-element array → return 1
  • Already sorted array → correctly finds full length

šŸ’” Other Approaches​

ApproachTimeSpaceNotes
Brute Force + SortO(n log n)O(1)āŒ Can't handle duplicates easily
HashSet (this)O(n)O(n)āœ… Best and optimal

šŸ’¬

Discussion & Doubts