Skip to content

Commit a338499

Browse files
committed
Add sum range queries problem
1 parent be1c909 commit a338499

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ List of Programs related to data structures and algorithms
6060
| 19 | Can place flowers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.md) | Easy | Array traversal and comparison |
6161
| 20 | Majority Element | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/20.majorityElement/majorityElement.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/20.majorityElement/majorityElement.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/20.majorityElement/majorityElement.md) | Easy | Boyer Moore Voting algorithm |
6262
| 21 | Pivot Index | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/21.pivotIndex/pivotIndex.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/21.pivotIndex/pivotIndex.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/21.pivotIndex/pivotIndex.md) | Easy | Array traversal |
63+
| 22 | Range Sum queries | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/22.sumRange/sumRange.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/22.sumRange/sumRange.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/22.sumRange/sumRange.md) | Easy | Array traversal |
6364

6465
### String
6566

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package java1.algorithms.array.sumRange;
2+
3+
public class SumRange {
4+
private int[] prefixSum;
5+
6+
SumRange(int[] nums) {
7+
this.prefixSum = new int[nums.length+1];
8+
9+
for (int i = 0; i < nums.length; i++) {
10+
this.prefixSum[i+1] = this.prefixSum[i] + nums[i];
11+
}
12+
}
13+
14+
private int sumRange(int left, int right){
15+
return this.prefixSum[right+1] - this.prefixSum[left];
16+
}
17+
18+
public static void main(String[] args) {
19+
int[] nums1 = new int[]{6, -3, 1, 5, 3, 7, -5};
20+
SumRange sumRangeInstance = new SumRange(nums1);
21+
System.out.println(sumRangeInstance.sumRange(0, 3));
22+
System.out.println(sumRangeInstance.sumRange(2, 6));
23+
System.out.println(sumRangeInstance.sumRange(0, 6));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
**Description:**
2+
Given an integer array `nums`, find the sum of the elements of `nums` between indices `left` and `right` inclusive where `left <= right`.
3+
4+
**Note:** The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of an array, the left sum becomes `0` because there are no left elements. It also applies to the right edge of an array. Return -1, if no such index found.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [6, -3, 1, 5, 3, 7, -5], left=0 right=3
10+
Output: 9
11+
12+
Example 2:
13+
14+
Input: nums = [6, -3, 1, 5, 3, 7, -5], left=2 right=6
15+
Output: 11
16+
17+
Example 3:
18+
19+
Input: nums = [6, -3, 1, 5, 3, 7, -5], left=0 right=6
20+
Output: 14
21+
22+
**Algorithmic Steps**
23+
This problem is solved with the help of array traversal and finding sum of the elements. The algorithmic approach can be summarized as follows:
24+
25+
1. Define a prefix sum variable(`prefixSum`) to calculate the sum of all elements with in a given array range.
26+
27+
2. The given input array needs to be iterated and store the prefix sum of each index position to avoid recomputing the sum for each given range.
28+
29+
3. Create a `sumRange` function/method to calculate the sum of elements with in given range. This is derived by substracting the left sum from right sum.
30+
31+
4. Invoke sumRange function with given `left` and `right` indices to find the sum of elements.
32+
33+
**Time and Space complexity:**
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once for constructing the prefix sum array.
35+
36+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than prefix sum variable.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class SumRange {
2+
constructor(nums) {
3+
this.prefixSum = Array(nums.length+1).fill(0);
4+
5+
for (let i = 0; i < nums.length; i++) {
6+
this.prefixSum[i+1] = this.prefixSum[i] + nums[i];
7+
}
8+
}
9+
10+
sumRange(left, right){
11+
return this.prefixSum[right+1] - this.prefixSum[left];
12+
}
13+
}
14+
15+
const nums1 = [6, -3, 1, 5, 3, 7, -5];
16+
const sumRangeInstance = new SumRange(nums1);
17+
console.log(sumRangeInstance.sumRange(0, 3));
18+
console.log(sumRangeInstance.sumRange(2, 6));
19+
console.log(sumRangeInstance.sumRange(0, 6));
20+
21+
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
**Description:**
2+
Given an integer array `nums`, find the sum of the elements of `nums` between indices `left` and `right` inclusive where `left <= right`.
3+
4+
**Note:** The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of an array, the left sum becomes `0` because there are no left elements. It also applies to the right edge of an array. Return -1, if no such index found.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [6, -3, 1, 5, 3, 7, -5], left=0 right=3
10+
Output: 9
11+
12+
Example 2:
13+
14+
Input: nums = [6, -3, 1, 5, 3, 7, -5], left=2 right=6
15+
Output: 11
16+
17+
Example 3:
18+
19+
Input: nums = [6, -3, 1, 5, 3, 7, -5], left=0 right=6
20+
Output: 14
21+
22+
**Algorithmic Steps**
23+
This problem is solved with the help of array traversal and finding sum of the elements. The algorithmic approach can be summarized as follows:
24+
25+
1. Define a prefix sum variable(`prefixSum`) to calculate the sum of all elements with in a given array range.
26+
27+
2. The given input array needs to be iterated and store the prefix sum of each index position to avoid recomputing the sum for each given range.
28+
29+
3. Create a `sumRange` function/method to calculate the sum of elements with in given range. This is derived by substracting the left sum from right sum.
30+
31+
4. Invoke sumRange function with given `left` and `right` indices to find the sum of elements.
32+
33+
**Time and Space complexity:**
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once for constructing the prefix sum array.
35+
36+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than prefix sum variable.

0 commit comments

Comments
 (0)