Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

13. Longest Consecutive Sequence

MediumDay 4 of 60Arrays & Hashing

Given an unsorted integer array nums, return the length of the longest run of consecutive integers (like 4, 5, 6, 7) whose values all appear in the array. The elements do not need to be next to each other in nums. Aim for O(n) time.

Example 1
Input: nums = [100,4,200,1,3,2]
Output: 4
The values 1, 2, 3, 4 are all present, a run of length 4.
Example 2
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Every value from 0 to 8 is present.
Constraints
  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Hints
Frequently asked at: Google, Amazon, Microsoft, Uber, PhonePe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min