BudiBadu Logo

Running Sum of Purchases

Array Easy 0 views
Like24

Tracking the cumulative revenue of a market ledger is a core task for any business. In Running Sum of Purchases, you’re given a list of purchase values. Your goal is to return a New List where each entry captures the total sum immediately after that transaction was processed. This offers an instant snapshot of how earnings (or refunds) accumulate moment by moment throughout the day!

The "secret sauce" here is Iterative Accumulation. You start with a running counter at zero. As you walk through the input list, you add the current purchase to your counter and store the result in your new array. If a value is negative (like a discount), it still joins the sequence and reduces the total. Empty lists should return an empty result set. This is a fundamental pattern for financial reporting and data analysis, turning a simple loop into a precise tool for monitoring progress and balance in real-time professional environments! Return the completed list for the market owner.

Examples

Example 1
Input
nums = [1,2,3,4]
Output
[1,3,6,10]
Explanation

The running totals after each purchase are 1, 3, 6, and 10.

Example 2
Input
nums = [1,1,1,1,1]
Output
[1,2,3,4,5]
Explanation

Each purchase adds 1, so the running sum increments steadily.

Example 3
Input
nums = [3,1,2,10,1]
Output
[3,4,6,16,17]
Explanation

The cumulative totals reflect how each purchase increases the balance.

Algorithm Flow

Recommendation Algorithm Flow for Running Sum of Purchases - Budibadu
Recommendation Algorithm Flow for Running Sum of Purchases - Budibadu

Best Answers

java
import java.util.*;
class Solution {
    public Object running_sum(Object nums) {
        int[] arr = (int[]) nums;
        List<Integer> r = new ArrayList<>(); int t = 0;
        for (int n : arr) { t += n; r.add(t); }
        return r;
    }
}