ExamAdda Logo

Running Sum of an Array

Easy

The Running Sum of an Array problem involves calculating the cumulative sum of elements in an array.

Given an integer array arr[] of size n, return an array where each element at index i represents the sum of all elements from index 0 to i.

The running sum at index i is calculated as:

runningSum[i] = arr[0] + arr[1] + ... + arr[i]

This problem helps in understanding Prefix Sum, cumulative computations, and efficient array traversal.

Example 1

Input

arr[] = [1,2,3,4]

Output

[1,3,6,10]

Explanation

The running sum is calculated as: 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 Hence, the resulting array is [1,3,6,10].

Example 2

Input

arr[] = [5,1,2,7]

Output

[5,6,8,15]

Explanation

Each element in the output array stores the cumulative sum of all elements up to that index.

Constraints

1 <= n <= 10^5
-10^9 <= arr[i] <= 10^9

Hints:

Hint 1
Maintain a variable to store the cumulative sum while traversing the array. Add the current element to it and store the updated sum in the result array.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Jul 2, 2026

Last updated

Jul 31, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output