You’re given an array of numbers called nums
. You need to return an array that consists of the running sums of the nums
array.
The running sum at position i
is the sum of all elements from the start of the array up to the i-th
element.
For example, if the array nums
is [1, 2, 3, 4]
, the running sum array would be [1, 3, 6, 10]
.
nums
?nums
? Are they all integers?nums
is an empty array? Should we return an empty array as well?nums
always guaranteed to have at least one element?nums
in-place, or should a new array be returned?def runningSum(nums):
result = []
running_sum = 0
for num in nums:
running_sum += num
result.append(running_sum)
return result
nums
. This is because we are iterating through the array once.nums
.nums = [1, 2, 3, 4]
print(runningSum(nums)) # Output: [1, 3, 6, 10]
nums = [1, 1, 1, 1, 1]
print(runningSum(nums)) # Output: [1, 2, 3, 4, 5]
nums = [3, 1, 2, 10, 1]
print(runningSum(nums)) # Output: [3, 4, 6, 16, 17]
In these examples, the function runningSum
correctly computes the running sums and returns the expected arrays.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?