97. Longest Consecutive Sequence in an Array

Given an array nums of n integers.

Return the length of the longest sequence of consecutive integers. The integers in this sequence can appear in any order.

Example 1:

Input: nums = [100, 4, 200, 1, 3, 2]

Output: 4

Explanation:

The longest sequence of consecutive elements in the array is [1, 2, 3, 4], which has a length of 4. This sequence can be formed regardless of the initial order of the elements in the array.

Example 2:

Input: nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1]

Output: 9

Explanation:

The longest sequence of consecutive elements in the array is [0, 1, 2, 3, 4, 5, 6, 7, 8], which has a length of 9. 

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 9, 3, 10, 4, 20, 2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  •      1 <= nums.length <= 105
  •      -109 <= nums[i] <= 109

Fun Facts

0
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
}
};
Test Case

Input:

Nums