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 <= 10^5
-10^9 <= nums[i] <= 10^9
When 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:
The brute force approach to finding the longest consecutive sequence in a list of numbers involves checking all possible sequences within the list. We consider each number as the potential start of a consecutive sequence and then try to build the sequence from there.
Here's how the algorithm would work step-by-step:
def longest_consecutive_sequence_brute_force(numbers):
longest_streak = 0
for number in numbers:
current_number = number
current_streak = 1
# Try to find the next numbers in the sequence
while True:
next_number = current_number + 1
if next_number in numbers:
# Increase the sequence length
current_streak += 1
current_number = next_number
else:
break
# Update the longest streak
if current_streak > longest_streak:
longest_streak = current_streak
return longest_streak
The core idea is to efficiently build the longest possible chain of consecutive numbers. We'll use a tool to quickly check if a number is present and only start building chains from numbers that are the beginning of a sequence.
Here's how the algorithm would work step-by-step:
def longestConsecutive(nums):
number_set = set(nums)
longest_streak = 0
for number in nums:
# Check if this is the start of a sequence
if (number - 1) in number_set:
continue
current_number = number
current_streak = 1
# Count how long the sequence is
while (current_number + 1) in number_set:
current_number += 1
current_streak += 1
# Update the longest streak found so far
longest_streak = max(longest_streak, current_streak)
return longest_streak
Case | How to Handle |
---|---|
Empty input array | Return 0 immediately as there's no sequence. |
Array with one element | Return 1, as a single element constitutes a sequence of length 1. |
Array with all identical numbers | The longest sequence length is 1 since elements must be strictly increasing. |
Array with consecutive numbers in reverse order | The solution should correctly identify the sequence when numbers are not pre-sorted. |
Array with duplicate numbers and some valid sequence | The solution must only count each number once in a sequence using a set to track visited elements. |
Array with negative and positive numbers forming a sequence | The solution should handle both positive and negative numbers correctly. |
Large input array with a very long consecutive sequence | The solution should use an efficient data structure like a set to ensure O(n) time complexity. |
Integer overflow if sequence contains very large numbers | Use appropriate data types and consider checking for potential overflow if necessary, though this is usually not directly relevant to the main algorithmic logic. |