We're given an array and need to produce a new array where each element at index i is the sum of all elements from index 0 through i in the original array. This is also called a prefix sum, a building block that appears in range-sum queries, subarray-sum problems, and 2D cumulative sums.
The first element stays the same, since it is the sum of itself. The second element is the sum of the first two, the third is the sum of the first three, and so on. Each running sum value builds on the previous one: once the running sum up to index i - 1 is known, the running sum at index i equals runningSum[i - 1] + nums[i]. That recurrence is what makes an efficient solution possible.
1 <= nums.length <= 1000 → With at most 1000 elements, even an O(n^2) approach stays well under typical time limits, so correctness matters more than shaving constants here.-10^6 <= nums[i] <= 10^6 → Values can be negative. The running sum can reach 10^9 in magnitude (1000 elements times 10^6 each), which still fits within a signed 32-bit integer (max about 2.1 x 10^9), so int does not overflow.For each index i, sum the elements from 0 to i. This follows the definition of running sum directly: every output position starts a fresh summation from the beginning of the array. It does redundant work, recomputing overlapping prefixes, but it is a correct baseline to improve on.
nums.i from 0 to n - 1:j from 0 to i, add nums[j] to the sum.result[i] to this sum.Take nums = [1, 2, 3, 4]. For each output index i, the inner loop re-adds every element from 0 to i, so the same early values are summed again at each step.
The brute force recomputes overlapping prefixes. The next approach reuses the previous result instead of summing from scratch each time.
The running sum at index i is the sum of elements from 0 to i, which expands to sum(0..i) = sum(0..i-1) + nums[i]. So each running sum equals the previous running sum plus the current element. That recurrence replaces the inner loop with a single addition.
We can compute the whole array in one pass. Keep a running total, add each element to it, and write that total to the corresponding output position. Because each value reuses the one already computed, the O(n^2) work collapses to O(n).
nums.result[0] = nums[0] (the first element has no previous sum to build on).i from 1 to n - 1:result[i] = result[i - 1] + nums[i].This approach runs in optimal time but uses O(n) extra space for the result array. If we are allowed to modify the input, we can drop that extra array and write the running sum back into nums itself.
When modifying the input array is acceptable, the result array is unnecessary. Iterate from index 1 and add the previous element to the current one. After the loop, nums holds the running sum, with no extra allocation.
Writing into the input array is safe only because of the left-to-right order. The loop maintains an invariant: before processing index i, every position 0 through i - 1 already holds its final running sum. When we run nums[i] += nums[i - 1], nums[i] still holds the original input value and nums[i - 1] already holds runningSum[i - 1], so the result is original_nums[i] + runningSum[i - 1] = runningSum[i]. Each position is read once after it is finalized and written once, so no value is overwritten before it is needed.
i from 1 to n - 1:nums[i - 1] to nums[i].nums.