Skip to main content
Back to problems
Leetcode
Easy
Arrays
Running Sum of 1D Array

Return the running sum of a one-dimensional array, where each position contains the sum of all elements up to that index.

Acceptance 0%
Problem Statement

Running Sum of a 1D Array

Given an integer array nums, build a new array runningSum such that runningSum[i] is the sum of the elements from index 0 through index i in nums.

In other words, each entry in the result should represent the cumulative total seen so far as you scan the array from left to right.

Input Format

  • An integer array nums of length n.

Output Format

  • Return an integer array runningSum of the same length, where runningSum[i] = nums[0] + nums[1] + ... + nums[i].

Constraints

  • 1n1 \le n.
  • The array length is moderate enough for a linear scan solution.
  • Elements may be positive, negative, or zero.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,2,3,4]

Output

[1,3,6,10]

Explanation

The cumulative sums are 1, 1+2=3, 1+2+3=6, and 1+2+3+4=10.

Example 2

Input

nums = [1,1,1,1,1]

Output

[1,2,3,4,5]

Explanation

Each position adds one more element to the running total.

Show 1 more example

Example 3

Input

nums = [3,1,2,10,1]

Output

[3,4,6,16,17]

Explanation

Keep adding the current value to the total seen so far.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.