Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

DSA›Arrays›Running Sum of 1D Array
EasyArrays

Running Sum of 1D Array

prefix-sumarray

Problem

Given an array nums, return the running sum, where runningSum[i] = sum(nums[0]...nums[i]).

Examples

Example 1

Input: nums = [1,2,3,4]

Output: [1,3,6,10]

Explanation: runningSum = [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: Each step adds exactly 1.

Constraints

  • •1 <= nums.length <= 1000
  • •-10^6 <= nums[i] <= 10^6

Hints

Hint 1

This is the prefix sum concept in its most literal form — the output array IS the sequence of prefix sums.

Hint 2

You don't need a separate output array allocated up front and filled in a second pass — you can build it in place.

Hint 3

Each output value only depends on the previous output value plus the current input — one running total, one pass.

Solutions

public int[] runningSum(int[] nums) {
    for (int i = 1; i < nums.length; i++) {
        nums[i] += nums[i - 1];
    }
    return nums;
}
Java

Time: O(n) · Space: O(1) extra (in-place)