Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9
Example 3:
Input: nums = [1,0,1,2] Output: 3
Constraints:
0 <= nums.length <= 105-109 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a messy pile of numbered cards and you want to find the longest chain of numbers that follow right after one another, like two, three, four, and five. The brute force approach works by picking every single number in your pile as a starting point and manually hunting through the rest of the pile to see how far the chain can go.
Here's how the algorithm would work step-by-step:
def longest_consecutive(numbers):
# We must track the longest chain found across all possible starting points
max_chain_length = 0
for starting_number in numbers:
current_chain_length = 1
current_number = starting_number
# We hunt through the collection repeatedly to extend our current chain
while (current_number + 1) in numbers:
current_number += 1
current_chain_length += 1
# We update our record whenever a checked chain beats the previous best
if current_chain_length > max_chain_length:
max_chain_length = current_chain_length
return max_chain_lengthInstead of sorting everything or checking every possible line starting from every single number, the optimal approach uses a quick lookup helper to only build sequences from their actual starting points. By ignoring numbers that are just middle pieces of a sequence, we save a massive amount of time.
Here's how the algorithm would work step-by-step:
def longest_consecutive(integer_list):
# Using a set provides O(1) lookups, crucial for checking existence efficiently.
unique_number_set = set(integer_list)
maximum_chain_length = 0
for current_number in unique_number_set:
# Skipping non-starting elements ensures we only evaluate valid sequence heads.
if (current_number - 1) not in unique_number_set:
active_number = current_number
current_chain_length = 1
while (active_number + 1) in unique_number_set:
active_number += 1
current_chain_length += 1
# We track the highest length found to avoid missing the longest sequence.
if current_chain_length > maximum_chain_length:
maximum_chain_length = current_chain_length
return maximum_chain_length| Case | How to Handle |
|---|---|
| Empty array or null input | Check for null or empty input at the start and return zero immediately. |
| Array with a single element | The algorithm skips the sequence-starting check and correctly returns a length of one. |
| Array containing duplicate numbers | Inserting elements into a set automatically de-duplicates them without affecting the sequence length calculation. |
| Array containing negative numbers and zero | The hash set handles negative integers and zero seamlessly because hash-based lookups work across the entire integer range. |
| Array with all identical elements | The set reduces all duplicates to a single value, and since no consecutive neighbors exist, the maximum length returned is one. |
| Extremely large array with no consecutive sequences | Using a hash set allows $O(N)$ time complexity, ensuring the solution scales efficiently without hitting time limits. |
| Disconnected sequences of varying lengths | The algorithm only initiates a count when it finds the true start of a sequence, avoiding redundant inner loops and maintaining $O(N)$ time. |
| Numbers near integer overflow limits | Using standard 32-bit or 64-bit integer addition when checking for num plus one requires care to prevent overflow in languages with strict bounds. |