Longest Subarray Whose Distinct Values Form Consecutive Integers

Quick Overview

Find the length of the longest contiguous subarray whose values are all distinct and form a run of consecutive integers in any order. Tests how to check a window in constant time, why a simple two-pointer window breaks here, and how to search many window starts at once on arrays of up to 100,000 values.

Longest Subarray Whose Distinct Values Form Consecutive Integers

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an integer array `nums`, find the length of the longest contiguous subarray whose elements form a set of consecutive integers. The elements in the subarray do not need to be in sorted order, but they must be consecutive when sorted (for example, `[3, 1, 2]` forms the consecutive set `{1, 2, 3}`). All elements in the subarray must be distinct for them to form a valid consecutive set. ### Function Signature ```python def longest_consecutive_subarray(nums: list[int]) -> int: ``` ### Rules - A subarray is a non-empty contiguous block `nums[i..j]` with `i <= j`. - A subarray qualifies when its values are pairwise distinct and, once sorted, each value is exactly 1 more than the one before it. A single element always qualifies, so the answer is at least 1. - Return only the length. ### Constraints - `1 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [2, 0, 2, 1, 4, 3, 1, 0] Output: 5 ``` The subarray `[0, 2, 1, 4, 3]` at indices 1 to 5 holds the distinct values `{0, 1, 2, 3, 4}`. Every subarray of length 6 or more repeats a value (the 2 at indices 0 and 2, or the 1 at indices 3 and 6), so 5 is the longest. ```text Input: nums = [5, 3, 4, 2, 1] Output: 5 ``` The whole array holds the distinct values `{1, 2, 3, 4, 5}`, so the answer is its full length. ```text Input: nums = [1, 2, 2, 3] Output: 2 ``` The values 1, 2 and 3 all appear, but every subarray longer than 2 contains both 2s. `[1, 2]` and `[2, 3]` each qualify with length 2.

Overview: Find the length of the longest contiguous subarray whose values are all distinct and form a run of consecutive integers in any order. Tests how to check a window in constant time, why a simple two-pointer window breaks here, and how to search many window starts at once on arrays of up to 100,000 values.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, find the length of the longest contiguous subarray whose elements form a set of consecutive integers.

The elements in the subarray do not need to be in sorted order, but they must be consecutive when sorted (for example, [3, 1, 2] forms the consecutive set {1, 2, 3}). All elements in the subarray must be distinct for them to form a valid consecutive set.

Function Signature

def longest_consecutive_subarray(nums: list[int]) -> int:

Rules

  • A subarray is a non-empty contiguous block nums[i..j] with i <= j .
  • A subarray qualifies when its values are pairwise distinct and, once sorted, each value is exactly 1 more than the one before it. A single element always qualifies, so the answer is at least 1.
  • Return only the length.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

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

The subarray [0, 2, 1, 4, 3] at indices 1 to 5 holds the distinct values {0, 1, 2, 3, 4}. Every subarray of length 6 or more repeats a value (the 2 at indices 0 and 2, or the 1 at indices 3 and 6), so 5 is the longest.

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

The whole array holds the distinct values {1, 2, 3, 4, 5}, so the answer is its full length.

Input: nums = [1, 2, 2, 3]
Output: 2

The values 1, 2 and 3 all appear, but every subarray longer than 2 contains both 2s. [1, 2] and [2, 3] each qualify with length 2.