945. Count of Range Sum (Merge Sort + BST)

HardTreesBITMerge SortPrefix Sum

Given an integer array and an inclusive range [lower, upper], return the number of range sums S(i, j) that lie within [lower, upper], where S(i, j) is the sum of elements from index i to j inclusive (i <= j). The input gives the array as a JSON array and 'lower upper' separated by ' | '.

Input: A JSON array and 'lower upper', separated by ' | '.

Output: Integer — the count of qualifying range sums.

Examples

Example 1
Input: [-2,5,-1] | -2 2
Output: 3
Explanation: Three range sums fall in [-2,2].
Example 2
Input: [0] | 0 0
Output: 1
Explanation: The single sum is 0.
Example 3
Input: [1,2,3] | 3 7
Output: 4
Explanation: Four qualifying sums.

Constraints

Asked by

InfosysGoogleAmazonMeta
Solve this problem in the editor →