Longest Window Whose Largest Pair Difference Meets a Limit

Quick Overview

Longest Window Whose Largest Pair Difference Meets a Limit. Return the length of the longest nonempty contiguous subarray of nums in which every pair of values differs in absolute value by at most limit.

Longest Window Whose Largest Pair Difference Meets a Limit

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the length of the longest nonempty contiguous subarray of nums in which every pair of values differs in absolute value by at most limit. Compare every pair, not only neighbors. ```cpp class Solution { public: int longestSubarray(vector<int>& nums, int limit); }; ``` ### Examples **Example 1:** ```text Input: nums = [1,3,2,6], limit = 2 Output: 3 Explanation: The window[1,3,2] has maximum3 minus minimum1 equal to2. The whole array fails. ``` **Example 2:** ```text Input: nums = [5,5,6,6,6], limit = 0 Output: 3 Explanation: Only equal-valued runs qualify; the last three6s are longest. ``` ### Constraints - nums has length 1 to 100000. - `1 <= nums[i] <= 1000000000`. - `0 <= limit <= 1000000000`. ### Contract considerations A singleton is always valid under the nonnegative limit. ```hint Read the boundary condition Every pair in the chosen subarray must meet the limit. Check a zero limit, repeated values, and singleton subarrays. ``` ### Boundary reminders A singleton is always valid under the nonnegative limit.

Overview: Longest Window Whose Largest Pair Difference Meets a Limit. Return the length of the longest nonempty contiguous subarray of nums in which every pair of values differs in absolute value by at most limit.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the length of the longest nonempty contiguous subarray of nums in which every pair of values differs in absolute value by at most limit. Compare every pair, not only neighbors.

class Solution {
public:
    int longestSubarray(vector<int>& nums, int limit);
};

Examples

Example 1:

Input: nums = [1,3,2,6], limit = 2
Output: 3
Explanation: The window[1,3,2] has maximum3 minus minimum1 equal to2. The whole array fails.

Example 2:

Input: nums = [5,5,6,6,6], limit = 0
Output: 3
Explanation: Only equal-valued runs qualify; the last three6s are longest.

Constraints

  • nums has length 1 to 100000.
  • 1 <= nums[i] <= 1000000000 .
  • 0 <= limit <= 1000000000 .

Contract considerations

A singleton is always valid under the nonnegative limit.

Boundary reminders

A singleton is always valid under the nonnegative limit.