Skip to content

Commit

Permalink
added recursion example with easy solution for beginners
Browse files Browse the repository at this point in the history
  • Loading branch information
officialabdulrehman committed Oct 23, 2021
1 parent 1f2bf95 commit 9bfdbef
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Recursion/sum_of_1d_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
NOTE: The provided solution is not copied
RECURSION Example in JavaScript
Problem: sum_of_1d_array ( from leetcode )
Explanation for problem: {
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of nums.
}
Demo:{
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
}
*/

// Test case [1,2,3,4]
// Expected output [1,3,6,10]

const array = [1, 2, 3, 4];

// Recursive Solution

const runningSum = (nums) => {
let result = [nums[0]];
const recursive = (loop) => {
if (nums.length <= loop) return;
else result.push(nums[loop] + result[loop - 1]);
recursive(loop + 1);
};
recursive(1);
return result;
};

runningSum(array); // function call

/*
Funtion runningSum takes an array of numbers as an argument
returns an array of running sum of given array
Note: recursive functions starts from index 1 of given array because we already assign index 0 value to result at the start
recursive function runs until the index given to recursive function is greater than equal to the length or array given to runningSum function
*/

0 comments on commit 9bfdbef

Please sign in to comment.