Longest Subarray
Learn this problemProblem statement
A subarray of array a is defined as a contiguous block of a's elements having a length that is less than or equal to the length of the array. For example, the subarrays of array a = [1, 2, 3] are [1], [2], [3], [1, 2], [2, 3], and [1, 2, 3]. Given an integer, k = 3, the subarrays having elements that sum to a number ≤ k are [1], [2], and [1, 2]. The longest of these subarrays is [1, 2], which has a length of 2. Given an array, a, determine its longest subarray that sums to less than or equal to a given value k.
Function
maxLength(a: int[], k: int) → int
Complete the function maxLength in the editor below. The function must return an integer that represents the length of the longest subarray of a that sums to a number ≤ k.
maxLength has the following parameter(s):
a[a[0],...a[n-1]]: an array of integersk: an integer
Examples
Example 1
a = [1, 2, 3]k = 4return = 2
The subarrays having elements that sum to a number ≤ 4 are [1], [2], and [1, 2]. The longest of these subarrays is [1, 2], which has a length of 2.
Constraints
1 ≤ n ≤ 10^51 ≤ a[i] ≤ 10^31 ≤ k ≤ 10^9
More Goldman Sachs problems
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jul 2026
- Data ReorganizationOA · Seen Jul 2026
- Inherited Role PermissionsONSITE INTERVIEW · Seen Jul 2026
- Root of the Largest TreePHONE SCREEN · Seen Jul 2026
- Alternating Parity PermutationsOA · Seen Jul 2026
- Threshold AlertsOA · Seen Jul 2026
- Cheapest Flights Within K StopsONSITE INTERVIEW · Seen Jun 2026
- Word LadderPHONE SCREEN · Seen Jun 2026