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] <= 109
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 strategy for finding the longest consecutive sequence involves checking every number in the input to see how long a sequence it can start. We consider each number as a potential beginning of a sequence and explore as far as we can.
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
# Iterate while the next number is in the set
while True:
next_number = current_number + 1
if next_number in numbers:
#If the next number exists, extend current streak
current_streak += 1
current_number = next_number
else:
break
# Update longest streak if needed
longest_streak = max(longest_streak, current_streak)
return longest_streak
The goal is to find the longest string of numbers in a given set where each number follows the next in order (like 1, 2, 3). The trick is to avoid doing unnecessary work by focusing only on the starting points of these sequences. We use a set to efficiently check if a number could be the start of such a sequence.
Here's how the algorithm would work step-by-step:
def longest_consecutive_sequence(numbers):
number_set = set(numbers)
longest_sequence_length = 0
for number in numbers:
# Skip if 'number' is part of a longer sequence already.
if (number - 1) in number_set:
continue
current_number = number
current_sequence_length = 1
# Build the sequence as long as the next number exists.
while (current_number + 1) in number_set:
current_number += 1
current_sequence_length += 1
# Update the longest sequence found so far.
longest_sequence_length = max(longest_sequence_length, current_sequence_length)
return longest_sequence_length
Case | How to Handle |
---|---|
Null or empty input array | Return 0 since there are no elements to form a consecutive sequence. |
Input array with only one element | Return 1 as a single element is a consecutive sequence of length one. |
Input array with all identical elements | Return 1 as the longest consecutive sequence is of length one (e.g., [2,2,2] should return 1). |
Input array with consecutive elements at the beginning and end, that when connected, form a longer sequence | The algorithm must correctly identify and merge overlapping sequences, like [1,2,3,7,8,9]. |
Input array with negative numbers | The algorithm should handle negative numbers correctly, and the presence of negative numbers should not affect the O(n) time complexity. |
Large input array with a wide range of integers (potential integer overflow) | Use a data structure like a hash set to avoid sorting and prevent integer overflow during comparisons. |
Input array with duplicates | Use a set to remove duplicate numbers and ensure each number is only considered once during sequence building. |
Maximum-sized input array with extremely long consecutive sequence. | Verify that the data structure used to store the numbers (e.g., hash set) can handle a large number of elements without excessive memory consumption or performance degradation. |