Longest Consecutive Sequence
To solve this coding challenge of finding the longest consecutive sequence in an unsorted array of integers, we can use the following approach. The main idea here is to utilize a hash set for O(1) average time complexity look-ups, ensuring that our overall algorithm can run in linear time, O(n).
Explanation:
- Initialization:
- First, if the array is empty, we return 0 immediately because there is no sequence in an empty array.
- Next, we convert the array to a set. This helps in ensuring O(1) time complexity for look-ups, which is crucial for maintaining linear time complexity.
- Finding the Starting Point of a Sequence:
-
For each number in the set, we check whether the number is the start of a potential sequence. This can be determined by checking if the number's predecessor is not in the set (i.e.,
not in the set). If it is not, then the current number could be the start of a new sequence.
num - 1 - Tracking the Sequence Length:
- For each potential starting number, we iterate to find the length of the consecutive sequence beginning with that number.
-
We keep a count of the current streak length and iterate through the next consecutive numbers (i.e.,
,
num + 1, etc.) until the next number is not found in the set.num + 2 - Updating the Longest Streak:
-
During this process, we continuously update the
variable with the maximum length of all sequences found so far.
longest_streak - Return the Result:
- Finally, we return the length of the longest consecutive sequence found.
- Handle Edge Case:
-
If the input array
is empty, return 0 immediately as there are no sequences in an empty array.
nums - Convert Array to Set:
-
Create a set
from
num_set. This conversion helps facilitate O(1) look-ups on average, which is critical for ensuring our algorithm runs in O(n) time.nums - Initialize Longest Streak Variable:
-
Create a variable
and set it to 0. This variable will keep track of the length of the longest consecutive sequence found during our iterations.
longest_streak - Iterate Through Each Number in the Set:
-
Loop through each
in
number.num_set - Identify the Start of a New Sequence:
-
For each
, check if
numberis not innumber - 1. If true, it meansnum_setcould be the start of a new sequence.number - Track Current Sequence:
-
Initialize
with
current_numandnumberwith 1.current_streak -
Use a while loop to check if
is in
current_num + 1. While it's true:num_set -
Increment
by 1.
current_num -
Increment
by 1.
current_streak - Update Longest Streak:
-
After exiting the while loop, update
with the maximum of
longest_streakandlongest_streak.current_streak - Return Result:
-
Finally, return the value of
as the length of the longest consecutive sequence.
longest_streak
# Function to find the longest consecutive sequence length
def findLongestConsecutiveSequence(nums):
# If the array is empty, return 0 immediately
if length of nums is 0:
return 0
# Convert array to a set to benefit from average O(1) time complexity look-ups
num_set = set(nums)
# Initialize the variable to keep track of the longest sequence length
longest_streak = 0
# Iterate through each number in the set
for number in num_set:
# If number - 1 is not in the set, then this might be the start of a sequence
if (number - 1) is not in num_set:
# Initialize variables for the current number and current streak length
current_num = number
current_streak = 1
# Continue to check for the next consecutive numbers in the sequence
while (current_num + 1) is in num_set:
# Move to the next number in the sequence
current_num += 1
# Increase the streak length
current_streak += 1
# Update the longest streak if the current streak is longer
longest_streak = maximum(longest_streak, current_streak)
# Return the length of the longest consecutive sequence found
return longest_streak