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 1:
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:
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:
Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17]
Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find a running total for a list of numbers. The simplest way is to calculate each running total individually by summing all the preceding numbers in the list each time.
Here's how the algorithm would work step-by-step:
def running_sum_brute_force(numbers):
running_totals = []
# Iterate through the input list to calculate running sums
for current_index in range(len(numbers)):
current_running_total = 0
# Calculate the sum from the beginning to current index
for previous_index in range(current_index + 1):
current_running_total += numbers[previous_index]
# Append current running total to the result
running_totals.append(current_running_total)
return running_totals
The goal is to efficiently calculate the running sum of a sequence of numbers. Instead of recalculating the sum each time, we reuse the previous calculation. This incremental approach significantly speeds up the process.
Here's how the algorithm would work step-by-step:
def runningSum(numbers):
running_sums = [0] * len(numbers)
# The first element is the initial running sum.
running_sums[0] = numbers[0]
for index in range(1, len(numbers)):
# Accumulate the sum using the previous running sum
running_sums[index] = running_sums[index - 1] + numbers[index]
return running_sums
Case | How to Handle |
---|---|
Null or empty input array | Return an empty array immediately to avoid processing invalid input. |
Array with a single element | Return the input array itself, as the running sum of a single element is the element itself. |
Array with large numbers that may cause integer overflow | Use a data type with a larger range (e.g., long) to store the running sum to prevent potential overflow. |
Array with a large number of elements (scalability) | The iterative approach inherently has O(n) time complexity and O(1) space complexity (excluding the input), scaling efficiently. |
Array with all elements being zero | The running sum will also be an array of zeros, which is a valid result. |
Array containing negative numbers | The solution should correctly handle negative numbers by subtracting them from the running sum. |
Array containing a mix of positive, negative, and zero values | The algorithm will correctly calculate cumulative sum regardless of sign of the individual numbers. |
Extreme boundary values for array elements (Integer.MAX_VALUE/MIN_VALUE) | Be cautious of potential integer overflow when adding to the running sum and use a larger datatype if needed. |