Javatpoint Logo
Javatpoint Logo

Count of Range Sum Problem in Java

An array numArr[] that contains negative as well as non-negative elements of the size S. Also, the two other numbers, 'left' and 'right', is provided. Our task is to return the number of the sum of the range, such that the sum is lying between the left and the right, including both (left and right), where left <= right. The sum of range sum R(l, r) shows the sum of numbers present between the index l to r in the numArr[], where l must be less than or equal to r.

Example 1:

Input: numArr[] = {1, 2, 3, 4, 5, 6, 7}, left = 2, right = 7

Output: There are 10 ranges whose sum lie between 2 & 7.

Explanation: Let's see the sum of the range of different subarrays.

Starting from the 0th index

R[0, 0] is 1 = 1     R[0, 1] = 1 + 2 = 3     R[0, 2] = 1 + 2 + 3 = 6

R[0, 3] = 1 + 2 + 3 + 4 = 10     R[0, 4] = 1 + 2 + 3 + 4 + 5 = 15

R[0, 5] = 1 + 2 + 3 + 4 + 5 + 6 = 21     R[0, 6] = 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

Starting from the 1st index

R[1, 1] is 2 = 2     R[1, 2] = 2 + 3 = 5&     R[1, 3] = 2 + 3 + 4 = 9

R[1, 4] = 2 + 3 + 4 + 5 = 14     R[1, 5] = 2 + 3 + 4 + 5 + 6 = 20

R[1, 6] = 2 + 3 + 4 + 5 + 6 + 7 = 27

Starting from the 2nd index

R[2, 2] is 3 = 3     R[2, 3] = 3 + 4 = 7     R[2, 4] = 3 + 4 + 5 = 12

R[2, 5] = 3 + 4 + 5 + 6 = 18     R[2, 6] = 3 + 4 + 5 + 6 + 7 = 25

Starting from the 3rd index

R[3, 3] is 4 = 4     R[3, 4] = 4 + 5 = 9     R[3, 5] = 4 + 5 + 6 = 15

R[3, 6] = 4 + 5 + 6 + 7 = 22

Starting from the 4th index

R[4, 4] is 5 = 5     R[4, 5] = 5 + 6 = 11     R[4, 6] = 5 + 6 + 7 = 18

Starting from the 5th index

R[5, 5] is 6 = 6     R[5, 6] = 6 + 7 = 13

Starting from the 6th index

R[6, 6] is 7 = 7

The following diagram shows the same.

Count of Range Sum Problem in Java

In all these ranges, only R[0, 1], R[0, 2], R[1, 1], R[1, 2], R[2, 2], R[2, 3], R[3, 3], R[4, 4], R[5, 5], and R[6, 6] lies between 2 to 7. Thus, the total count of sum of range that lies between 2 and 7 (both inclusive) is 7. Hence, the answer is 7.

Example 2:

Input: numArr[] = {11, 12, 13, 14, 15, 16, 17}, left = 9, right = 10

Output: There is no range that has the sum lying between 13 & 20.

Explanation: Let's see the sum of the range of different subarrays.

Starting from the 0th index

R[0, 0] is 11 = 11     R[0, 1] = 11 + 12 = 23     R[0, 2] = 11 + 12 + 13 = 36

R[0, 3] = 11 + 12 + 13 + 14 = 50     R[0, 4] = 11 + 12 + 13 + 14 + 15 = 65

R[0, 5] = 11 + 12 + 13 + 14 + 15 + 16 = 81 R[0, 6] = 11 + 12 + 13 + 14 + 15 + 16 + 17 = 98

Starting from the 1st index

R[1, 1] is 12 = 12     R[1, 2] = 12 + 13 = 25     R[1, 3] = 12 + 13 + 14 = 39

R[1, 4] = 12 + 13 + 14 + 15 = 54     R[1, 5] = 12 + 13 + 14 + 15 + 16 = 70

R[1, 6] = 12 + 13 + 14 + 15 + 16 + 17 = 87

Starting from the 2nd index

R[2, 2] is 13 = 13     R[2, 3] = 13 + 14 = 27     R[2, 4] = 13 + 14 + 15 = 42

R[2, 5] = 13 + 14 + 15 + 16 = 58     R[2, 6] = 13 + 14 + 15 + 16 + 17 = 75

Starting from the 3rd index

R[3, 3] is 14 = 14     R[3, 4] = 14 + 15 = 29     R[3, 5] = 14 + 15 + 16 = 45

R[3, 6] = 14 + 15 + 16 + 17 = 62

Starting from the 4th index

R[4, 4] is 15 = 15     R[4, 5] = 15 + 16 = 31     R[4, 6] = 15 + 16 + 17 = 48

Starting from the 5th index

R[5, 5] is 16 = 16     R[5, 6] = 16 + 17 = 33

Starting from the 6th index

R[6, 6] is 17 = 17

The following diagram shows the same.

Count of Range Sum Problem in Java

In all these ranges, there is no range whose sum lies between 9 to 10. Thus, the total count of the sum of the range that lies between 9 and 10 (both inclusive) is 0.

Naïve Approach

The straightforward or the naïve approach is to find all of the subarrays of the input array. After that, filter out those subarrays whose sum lies in the range (left to right).

Algorithm

Step 1: Initialize the variable count = 0, tmp = 0.

Step 2: Iterate j from 0 to the length of the input array - 1. It is done to determine the beginning point of our subarrays.

  • Update tmp = 0 in each iteration of this loop.
  • Iterate i from j to the length of the input array - 1. It will determine the ending point of our subarrays.
  • Update tmp as tmp = tmp + numArr[i];
  • If tmp >= left or tmp <= right, then increment the count variable by 1.

Step 3: Return the count.

Implementation

Observe the implementation of the above algorithm.

FileName: CountRangeSum.java

Output:

For the input array: 
1 2 3 4 5 6 7 
The number of ranges whose sum lies between 2 & 7 is 10.

For the input array: 
11 12 13 14 15 16 17 
There is no range whose sum lies between 9 & 10.

Complexity Analysis: We have used nested for-loops for computing the answer. Hence, the time complexity of the program is O(n2), where n is the total number of elements present in the array. Also, we have not used any data structure for keeping the results. Thus the space complexity of the program is constant, i.e., O(1).

Efficient Approach

In this approach, we will try to reduce the time complexity of the program from O(n2). In this solution, with the help of 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, it is required to compute the two indices m and n in the right half, where n is the first index satisfy sumArr[n] - sumArr[i] <= right and k is the first index satisfy sumArr[m] - sumArr[i] < left. Then, the total number of sums in [left, right] is n - m.

Observe the following algorithm.

Algorithm

Step 1: Define a method that counts the sub-arrays whose sum lies in the range [left, right] using merge sort, which is int mergeCount(int sumArr[], int left, int right, int st, int ed).

Step 2: Handle the base case: when ed - st <= 1, return 0.

Step 3: Initialize some variables int mid = (st + ed) / 2; int m = mid; int n = mid;

Step 4: Intialize another variable called count, and update the count as, count = mergeCount (sumArr, left, right, st, mid) + mergeCount(sumArr, left, right, mid, ed)

Step 5: Iterate j from st to mid.

While(m < ed and sumArr[m] - sumArr[j] < left) m = m + 1;

while(n < ed and sumArr[n] - sumArr[j] <= right) n = n + 1;

Update count as count = count + n - m.

Step 6: Sort sumArr from st to ed.

Step 7: Return the count.

Implementation

Observe the implementation of the above algorithm.

FileName: CountRangeSum1.java

Output:

For the input array: 
1 2 3 4 5 6 7 
The number of ranges whose sum lies between 2 & 7 is 10.

For the input array: 
11 12 13 14 15 16 17 
There is no range whose sum lies between 9 & 10.

Complexity Analysis: We have used the merge sort technique. Hence, the time complexity of the above program is O(n * log(n)). Also, we have used auxiliary array sumArr[]. Hence, the space complexity of the above program is O(n), where n is the total number of the element present in the input array.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA