


Let nums = [1, 2, 3], L = 2, R = 7
Then the possible ranges are [1,1],[2,2],[0,1],[1,2],[0,2], and their sum is 2,3,3,5,6 respectively. Hence the answer will be 5.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains an integer N.
The second line of each test case contains N space-separated integers denoting the elements of the array.
The third line of each test case contains two space-separated integers L and R.
For each test case, return an integer representing the number of range sum.
Output for each query is printed in a separate line.
Note: You don’t need to print anything or take input. This already has been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^4
-2^31 <= Nums[i] <= 2^31-1
-3*10^4 <= L <= R <= 3*10^4
where T is the number of test cases, 'N' is the number of elements in the array, Num[i] is the elements of the array num, and 'L' and 'R' are the given range.
Time Limit: 1 sec
The naive approach to this problem will check all subarrays of Nums and if their range lies between L and R, then increase the count.
Algorithm:
Approach:
In this solution using merge sort, we count the answer while doing the merge. During the merge stage, we have already sorted the left half [start, mid) and right half [mid, end).
We then iterate through the left half with index i. For each i, we need to find two indices k and j in the right half where j is the first index satisfy sums[j] - sums[i] > R and k is the first index satisfy sums[k] - sums[i] >= L.
Then the number of sums in [L, R] is j - k.
Algorithm:
Fair Value Distribution
Longest Substring with K-Repeating Characters
Student Percentile Calculation
Player Leaderboard Sorting
Minimized Maximum of Products Distributed to Any Store