Introduction
The Running Sum problem involves calculating the cumulative sum of elements in an array.
The running sum at index i is equal to:
- sum of all elements from index 0 to i.
Given an array arr[] of size n, the task is to return the running sum array.
This is one of the most important beginner-level Prefix Sum problems and helps in understanding cumulative computations, prefix processing, and array traversal.
Example
Input: arr[] = [1, 2, 3, 4]Output: [1, 3, 6, 10]
Explanation:
Running Sum at index 0 = 1
Running Sum at index 1 = 1 + 2 = 3
Running Sum at index 2 = 1 + 2 + 3 = 6
Running Sum at index 3 = 1 + 2 + 3 + 4 = 10
Input: arr[] = [5, 1, 2, 7]
Output: [5, 6, 8, 15]
Explanation:
Each element stores cumulative sum till that index.
Constraints
1 <= n <= 10^5-10^9 <= arr[i] <= 10^9
Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- For every index:
- calculate sum from index 0 to current index
- Store the calculated sum
This approach is easy to understand but repeatedly calculating sums increases time complexity.
Steps
- Traverse the array.
- For every index:
- calculate cumulative sum from beginning
- Store the sum in result array.
- Return result array.
Dry Run
Input Array: [1, 2, 3, 4]Index 0:
Sum = 1
Result:
[1]
Index 1:
Sum = 1 + 2 = 3
Result: [1, 3]
Index 2:
Sum = 1 + 2 + 3 = 6
Result:
[1, 3, 6]
Index 3:
Sum = 1 + 2 + 3 + 4 = 10
Final Result:
[1, 3, 6, 10]
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
For every index, previous elements are traversed again.
Space Complexity: O(n)
Explanation:
An extra array is used to store running sums.
Approach 2 : Optimized Solution (Prefix Sum)
Explanation
Instead of recalculating sums repeatedly, we can use the Prefix Sum Technique.
The idea is:
- Running Sum at current index =
- previous running sum + current element
This allows cumulative sums to be calculated efficiently in one traversal.
Steps
- Initialize:
- runningSum = 0
- Traverse the array.
- Add current element to runningSum.
- Store runningSum into result array.
- Return final result.
Dry Run
Input Array: [1, 2, 3, 4]Initially:
runningSum = 0
Traverse 1:
runningSum = 0 + 1 = 1
Result:
[1]
Traverse 2:
runningSum = 1 + 2 = 3
Result:
[1, 3]
Traverse 3:
runningSum = 3 + 3 = 6
Result:
[1, 3, 6]
Traverse 4:
runningSum = 6 + 4 = 10
Final Result:
[1, 3, 6, 10]
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation: Each element is processed only once.
Space Complexity: O(n)
Explanation: Result array stores cumulative sums.
Edge Cases
- Array contains negative numbers
- Array contains zeros
- Array contains one element
- Array contains large values
Why This Problem is Important
This problem helps in understanding:
- Prefix Sum Technique
- Cumulative calculations
- Efficient traversal
- Range-based computations
- Optimization techniques
It is one of the most important beginner-level Prefix Sum problems.
Real-World Applications
Running Sum concepts are used in:
- Financial analytics
- Data stream processing
- Time-series analysis
- Sensor monitoring
- Performance tracking systems
Common Mistakes
- Incorrect cumulative updates
- Forgetting previous sum
- Incorrect initialization
- Overwriting original values unintentionally
Interview Tips
Interviewers often expect:
- Prefix Sum optimization
- O(n) traversal
- Proper cumulative logic
Always explain why storing previous cumulative sums avoids repeated calculations.
Related Questions
- Range Sum Query
- Subarray Sum Equals K
- Prefix Sum Array
- Maximum Subarray Sum
- Product Except Self
Final Takeaway
The Running Sum problem is a fundamental Prefix Sum problem that teaches cumulative computation and efficient traversal techniques. Understanding this problem builds a strong foundation for advanced prefix sum, range query, and subarray interview problems.