Z12
Longest Consecutive Sequence
Problem Statement & Objective
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.
Function Signature (Python)
longest_consecutive_sequence(nums: list[int]) -> intInput & Output Contract
Output Rule:Length of the longest consecutive sequence.
System Constraints
0 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9Example Execution Walkthrough
nums = [100, 4, 200, 1, 3, 2]
longest sequence is [1, 2, 3, 4] -> length 4Example Test Scenarios (3)
Example 1
Input:
[[100,4,200,1,3,2]]Expected Output:
4Example 2: 0..8
Input:
[[0,3,7,2,5,8,4,6,0,1]]Expected Output:
9Example 3: Boş Dizi
Input:
[[]]Expected Output:
0