Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript
Consider the following array of numbers:
const arr = [10, 5, 6, 12, 7, 1];
The sum of its consecutive elements taking one less element in every go will be:
[10, 5, 6, 12, 7, 1] = 10 + 5 + 6 + 12 + 7 + 1 = 41; [5, 6, 12, 7, 1] = 5 + 6 + 12 + 7 + 1 = 31; [6, 12, 7, 1] = 6 + 12 + 7 + 1 = 26; [12, 7, 1] = 12 + 7 + 1 = 20; [7, 1] = 7 + 1 = 8; [1] = 1 = 1;
So, the final output should be an array like this:
[ 41, 31, 26, 20, 8, 1 ]
We are required to write a function that takes in one such array and returns the partialSum array back as illustrated in the example above.
Using map() and reduce() together
The idea here is simple. Since we are required to return one particular element for each and every element in the array, we can use the Array.prototype.map() method which does exactly this for us.
If we make the map() method return the reduced sum of the required elements by comparing the particular indices, we will get the work done.
const arr = [10, 5, 6, 12, 7, 1];
const partSum = arr.map((item, index) => {
return arr.reduce((acc, val, ind) => {
return ind >= index ? acc+val : acc;
}, 0);
});
console.log(partSum);
[ 41, 31, 26, 20, 8, 1 ]
Using Recursive Functions
Here we will make use of two recursive functions:
sumRecursively(arr, start)- Returns the sum of the elements of arr from the index start till the very end.partSumRecursively()- Recursively concatenates the required sum into an array and when we reach the end of the array, it returns the concatenated array.
const arr = [10, 5, 6, 12, 7, 1];
const sumRecursively = (arr, start = 0, res = 0) => {
if(start < arr.length){
return sumRecursively(arr, start+1, res+arr[start]);
};
return res;
};
const partSumRecursively = (arr, partSum = [], start = 0, end = arr.length-1) => {
if(start <= end){
return partSumRecursively(arr, partSum.concat(sumRecursively(arr, start)), ++start, end);
};
return partSum;
};
console.log(partSumRecursively(arr));
[ 41, 31, 26, 20, 8, 1 ]
How the Recursive Approach Works
The recursive solution works in two stages:
- sumRecursively: Calculates the sum from a given starting index to the end of the array using recursion.
-
partSumRecursively: Iterates through each starting position and calls
sumRecursivelyto get the partial sum, then recursively builds the result array.
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| map() + reduce() | O(n²) | O(1) | High |
| Recursive | O(n²) | O(n) | Medium |
Conclusion
Both approaches solve the prefix sum problem effectively. The map/reduce method is more readable, while the recursive approach demonstrates functional programming concepts. Choose based on your preference for imperative vs functional style.
