Count of Range Sum

Given an integer array nums and two integers lower and upper, count the number of range sums whose values lie in the inclusive interval [lower, upper]. This is a well-known advanced Fenwick Tree problem after converting prefix sums into ordered ranks.

Input Format

nums = integer array, lower and upper = inclusive sum bounds

Output Format

number of subarrays with sum in [lower, upper]

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= lower <= upper <= 10^9

Examples

Example 1:

Input:

nums = [-2,5,-1]
lower = -2
upper = 2

Output:

3

Explanation:

The valid subarrays are [-2], [-2,5,-1], and [-1].

Example 2:

Input:

nums = [0]
lower = 0
upper = 0

Output:

1

Explanation:

The single subarray [0] has sum 0, which lies in the target range.

Loading...
Count of Range Sum - Advanced Trees DSA Problem