Running Sum of 1D Array
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.
Problem Statement
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
.
Example
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].
Solution
From the example shared above, let's compare the numbers present in nums
in the input and the output.

You can see here, that we update each index in nums
by adding the value present at the previous index i.e. nums[i] = nums[i] + nums[i-1]
where i
is the index starting from 1 to len(nums)
.
Let's now implement this approach.
Python
This solution has a time complexity of O(N)
since we iterate over the entire array and has a space complexity of O(1)
since we use no additional space.
If you have any questions, feel free to drop a comment and we'll get back to you as soon as possible.