 All Problems
Number of Subarrays with Bounded Maximum
medium
sliding window
arrays
google
amazon

Given an integer array nums and two integers left and right, return the number of contiguous non-empty subarrays such that the value of the maximum array element in that subarray is in the range [left, right].

Example 1:

Input: 2 1 4 3
left: 2
right: 3
Output: 3
Explanation: Subarrays: [2], [2,1], [3].

Example 2:

Input: 2 9 2 5 6
left: 2
right: 8
Output: 7

Constraints:

  • 1 ≤ nums.length ≤ 2 × 10⁴
  • 0 ≤ nums[i] ≤ 10⁹
  • 0 ≤ left ≤ right ≤ 10⁹

Input format: First line: space-separated array. Second line: left. Third line: right.

Output format: Count of subarrays.

Run to check your code against the sample cases, or submit to run every case