Category : Easy

**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.

https://leetcode.com/problems/running-sum-of-1d-array

**Examples**

**Example 1:**

1 2 3 | 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]. |

**Example 2**:

1 2 3 | Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. |

**Example 3:**

1 2 | Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17] |

**Constraints**

`1 <= nums.length <= 1000`

`-10^6 <= nums[i] <= 10^6`

**Solution Approach**

In this question, you just need to find the running sum i.e the at any index of the array the sum should be equal to the total sum of all the previous indexes.

The solution to this problem is pretty straightforward. You just need to create a new array and its 0th index should have the value which is present at the 0th index in the original array. This is also supported by the fact that there will be at least one element in the array as stated in the problem. Now loop through each element in the array starting from 1 and keep adding the previous index’s value with the current value at the same index in the array given. It is a simple and easy example of Dynamic Programming.

**Solution code**

1 2 3 4 5 6 7 8 9 10 | class Solution { public int[] runningSum(int[] nums) { int[] answer = new int[nums.length]; answer[0] = nums[0]; for(int i=1; i<nums.length; i++){ answer[i] = answer[i-1] + nums[i]; } return answer; } } |

For more Leetcode explained solutions visit Leetcode Solutions. If you like capture the flag challenges visit here.

Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via Contact page I will surely respond. Happy Leetcoding