533. Count of Range Sum

HardBinary SearchBinary SearchMerge SortPrefix Sum

Given an array and a range [lower, upper], count the number of contiguous subarrays whose sum lies within [lower, upper] inclusive. Use prefix sums with a merge-sort or BIT counting approach. The input is JSON {nums, lower, upper}. Return the count.

Input: JSON {nums, lower, upper}.

Output: Integer — the number of qualifying range sums.

Examples

Example 1
Input: {"nums":[-2,5,-1],"lower":-2,"upper":2}
Output: 3
Explanation: Three subarrays have sums in [-2,2].
Example 2
Input: {"nums":[0],"lower":0,"upper":0}
Output: 1
Explanation: Single zero subarray.
Example 3
Input: {"nums":[1,2,3],"lower":3,"upper":5}
Output: 3
Explanation: Subarrays summing within [3,5].

Constraints

Asked by

InfosysGoogleAmazonMeta
Solve this problem in the editor →