Prefix Sum Array
📖 Scenario: Imagine you have a list of daily sales numbers for a small shop. You want to quickly find the total sales from the start up to any given day.
🎯 Goal: You will build a prefix sum array that stores the running total of sales up to each day. This helps answer total sales queries efficiently.
📋 What You'll Learn
Create an integer array called
sales with exactly these values: 5, 3, 8, 6, 2Create an integer array called
prefix_sum with the same length as salesCreate an integer variable called
n and set it to the length of salesUse a
for loop with variable i from 0 to n - 1 to fill prefix_sumPrint the
prefix_sum array elements separated by spaces💡 Why This Matters
🌍 Real World
Prefix sums are used in finance to quickly calculate cumulative totals like sales or expenses over time.
💼 Career
Understanding prefix sums helps in data analysis, algorithm optimization, and solving range query problems efficiently.
Progress0 / 4 steps
