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.
Auto
Loading editor...
Input

Expected Output