hardArrays & HashingAI-applied~50 min
Count Score Windows Whose Total Falls In A Band
A model emits a stream of signed reward deltas. For calibration you need to count how many contiguous windows have a total reward within a target band [lower, upper] — useful for spotting how often cumulative drift stays inside tolerance.
Problem
Given an integer array nums and two integers lower and upper, return the number of contiguous subarrays whose sum lies in the inclusive range [lower, upper].
Input
An integer array nums and integers lower and upper.
Output
The count of qualifying subarrays.
Constraints
- 1 ≤ n ≤ 1e5
- -2^31 ≤ nums[i] ≤ 2^31 - 1
- -1e5 ≤ lower ≤ upper ≤ 1e5 (range of allowed sums may exceed int; use 64-bit)
Examples
Example 1
Input
nums=[-2,5,-1] lower=-2 upper=2
Output
3
Subarrays [-2], [-1], [-2,5,-1] have sums -2, -1, 2 — all in range.
Example 2
Input
nums=[0] lower=0 upper=0
Output
1
The single subarray [0] sums to 0.