algos_and_structures/neetcode/arrays/longest_consecutive_sequence
2025-02-14 21:17:06 +03:00
..
README.md Add solution for LL reverse 2025-02-14 21:17:06 +03:00
solution.py Add solution for LL reverse 2025-02-14 21:17:06 +03:00

Longest Consecutive Sequence Given an array of integers nums, return the length of the longest consecutive sequence of elements that can be formed.

A consecutive sequence is a sequence of elements in which each element is exactly 1 greater than the previous element. The elements do not have to be consecutive in the original array.

You must write an algorithm that runs in O(n) time.

Example 1:

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

Output: 4 Explanation: The longest consecutive sequence is [2, 3, 4, 5].

Example 2:

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

Output: 7 Constraints:

0 <= nums.length <= 1000 -10^9 <= nums[i] <= 10^9

Solution

[2,20,4,10,3,4,5]

1

e = 2 2

2

e = 20 2], [20

3

e = 4 2], [20], [4

4

e = 10 2], [20], [4], [10

5

e = 3 2,3], [20], [4], [10

6

e = 4 2,3,4], [20], [4], [10

7

e = 5 2,3,4,5], [20], [4], [10