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.,
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
num + 2
- Updating the Longest Streak:
-
During this process, we continuously update the
longest_streak
- Return the Result:
- Finally, we return the length of the longest consecutive sequence found.
- Handle Edge Case:
-
If the input array
nums
- Convert Array to Set:
-
Create a set
num_set
nums
- Initialize Longest Streak Variable:
-
Create a variable
longest_streak
- Iterate Through Each Number in the Set:
-
Loop through each
number
num_set
- Identify the Start of a New Sequence:
-
For each
number
number - 1
num_set
number
- Track Current Sequence:
-
Initialize
current_num
number
current_streak
-
Use a while loop to check if
current_num + 1
num_set
-
Increment
current_num
-
Increment
current_streak
- Update Longest Streak:
-
After exiting the while loop, update
longest_streak
longest_streak
current_streak
- Return Result:
-
Finally, return the value of
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