304 North Cardinal St.
Dorchester Center, MA 02124

# Count of Range Sum LeetCode Solution

## Problem – Count of Range Sum LeetCode Solution

Given an integer array `nums` and two integers `lower` and `upper`, return the number of range sums that lie in `[lower, upper]` inclusive.

Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.

Example 1:

``````Input: nums = [-2,5,-1], lower = -2, upper = 2
Output: 3
Explanation: The three ranges are: [0,0], [2,2], and [0,2] and their respective sums are: -2, -1, 2.
``````

Example 2:

``````Input: nums = [0], lower = 0, upper = 0
Output: 1
``````

Constraints:

• `1 <= nums.length <= 105`
• `-231 <= nums[i] <= 231 - 1`
• `-105 <= lower <= upper <= 105`
• The answer is guaranteed to fit in a 32-bit integer.

## Count of Range Sum LeetCode Solution in Java

``````public int countRangeSum(int[] nums, int lower, int upper) {
int n = nums.length;
long[] sums = new long[n + 1];
for (int i = 0; i < n; ++i)
sums[i + 1] = sums[i] + nums[i];
int ans = 0;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j <= n; ++j)
if (sums[j] - sums[i] >= lower && sums[j] - sums[i] <= upper)
ans++;
return ans;
}
``````

## Count of Range Sum LeetCode Solution in C++

``````class Solution {
public:
int mergeSort(vector<long>& sum, int lower, int upper, int low, int high)
{
if(high-low <= 1) return 0;
int mid = (low+high)/2, m = mid, n = mid, count =0;
count =mergeSort(sum,lower,upper,low,mid) +mergeSort(sum,lower,upper,mid,high);
for(int i =low; i< mid; i++)
{
while(m < high && sum[m] - sum[i] < lower) m++;
while(n < high && sum[n] - sum[i] <= upper) n++;
count += n - m;
}
inplace_merge(sum.begin()+low, sum.begin()+mid, sum.begin()+high);
return count;
}

int countRangeSum(vector<int>& nums, int lower, int upper) {
int len = nums.size();
vector<long> sum(len + 1, 0);
for(int i =0; i< len; i++) sum[i+1] = sum[i]+nums[i];
return mergeSort(sum, lower, upper, 0, len+1);
}
};
``````

## Count of Range Sum LeetCode Solution in Python

``````    def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
cumsum = [0]
for n in nums:
cumsum.append(cumsum[-1]+n)

import collections
record = collections.defaultdict(int)

res = 0
for csum in cumsum:
for target in range(lower,upper+1):
if csum - target in record:
res += record[csum - target]
record[csum] +=1
return res
``````
##### Count of Range Sum LeetCode Solution Review:

In our experience, we suggest you solve this Count of Range Sum LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Count of Range Sum LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Count of Range Sum LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions