FastPrepLongest Consecutive Sequence

Longest Consecutive Sequence

EPAM logoEPAM● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an unsorted integer array nums, return the length of the longest sequence of consecutive integer values.

The values of a sequence do not need to appear consecutively in the input. Design an algorithm with expected O(n) time.

Function

longestConsecutive(nums: int[]) → int

Examples

Example 1

nums = [100,4,200,1,3,2]return = 4

The longest sequence is 1,2,3,4.

Example 2

nums = [0,3,7,2,5,8,4,6,0,1]return = 9

The values from 0 through 8 are all present.

Constraints

  • 0 <= nums.length <= 100000.
  • Each value is in [-10^9, 10^9].

Source note: Author-written prompt excerpt retained for source fidelity.

More EPAM problems

See EPAM hiring insights
public int longestConsecutive(int[] nums) {
    // Write your code here.
}
nums[100,4,200,1,3,2]
expected4
Checking account…