Longest Subarray with Sum at Most K in a Non-Negative Array

Quick Overview

Find the length of the longest contiguous subarray of non-negative integers whose sum stays at or below a limit k, returning 0 when none fits. It tests the two-pointer sliding window, why that window is valid only when values cannot be negative, and linear-time bookkeeping of the window sum.

Longest Subarray with Sum at Most K in a Non-Negative Array

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of non-negative integers `nums` and an integer `k`, return the length of the longest contiguous subarray whose sum is less than or equal to `k`. A subarray is a contiguous, non-empty sequence of elements within the array. If no subarray has a sum of at most `k`, return `0`. ### Function Signature ```python def longest_subarray(nums: list[int], k: int) -> int: ``` ### Rules - A subarray is `nums[i..j]` for some `i <= j`, taken without gaps or reordering. - Only the length is returned, so it does not matter which of several longest subarrays you find. - Return `0` when no subarray qualifies, which happens exactly when every element is larger than `k`. ### Constraints - `1 <= nums.length <= 10^5` - `0 <= nums[i] <= 10^4` - `0 <= k <= 10^9` ### Examples ```text Input: nums = [1,2,1,0,1,1,0], k = 4 Output: 5 ``` The whole array sums to 6, and dropping either end element still leaves a sum of 5 or 6, so no subarray of length 6 or 7 fits. The subarray `[1,0,1,1,0]` (indices 2 to 6) sums to 3, which gives length 5. ```text Input: nums = [5,6,7,8], k = 4 Output: 0 ``` Every element is larger than 4, and with no negative values a subarray's sum is at least its largest element, so no subarray qualifies. ```text Input: nums = [0,0,1,0], k = 0 Output: 2 ``` With `k = 0` only runs of zeros qualify. The longest run is `[0,0]` at indices 0 and 1.

Overview: Find the length of the longest contiguous subarray of non-negative integers whose sum stays at or below a limit k, returning 0 when none fits. It tests the two-pointer sliding window, why that window is valid only when values cannot be negative, and linear-time bookkeeping of the window sum.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of non-negative integers nums and an integer k, return the length of the longest contiguous subarray whose sum is less than or equal to k.

A subarray is a contiguous, non-empty sequence of elements within the array. If no subarray has a sum of at most k, return 0.

Function Signature

def longest_subarray(nums: list[int], k: int) -> int:

Rules

  • A subarray is nums[i..j] for some i <= j , taken without gaps or reordering.
  • Only the length is returned, so it does not matter which of several longest subarrays you find.
  • Return 0 when no subarray qualifies, which happens exactly when every element is larger than k .

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^4
  • 0 <= k <= 10^9

Examples

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

The whole array sums to 6, and dropping either end element still leaves a sum of 5 or 6, so no subarray of length 6 or 7 fits. The subarray [1,0,1,1,0] (indices 2 to 6) sums to 3, which gives length 5.

Input: nums = [5,6,7,8], k = 4
Output: 0

Every element is larger than 4, and with no negative values a subarray's sum is at least its largest element, so no subarray qualifies.

Input: nums = [0,0,1,0], k = 0
Output: 2

With k = 0 only runs of zeros qualify. The longest run is [0,0] at indices 0 and 1.