0
0
DSA Pythonprogramming~30 mins

Prefix Sum Array in DSA Python - Build from Scratch

Choose your learning style9 modes available
Prefix Sum Array
📖 Scenario: You are working with daily sales data for a small shop. You want to quickly find the total sales from the start of the month up to any given day.
🎯 Goal: Build a prefix sum array that stores cumulative sales totals for each day. This will help you answer total sales queries efficiently.
📋 What You'll Learn
Create a list of daily sales numbers
Create a prefix sum list to store cumulative totals
Use a loop to fill the prefix sum list
Print the prefix sum list
💡 Why This Matters
🌍 Real World
Prefix sum arrays are used in finance, data analysis, and gaming to quickly calculate totals over ranges without repeated addition.
💼 Career
Understanding prefix sums helps in optimizing algorithms and is a common interview topic for software engineering roles.
Progress0 / 4 steps
1
Create the daily sales list
Create a list called daily_sales with these exact values: [10, 20, 15, 30, 25]
DSA Python
Hint

Use square brackets and commas to create the list.

2
Create the prefix sum list
Create a list called prefix_sum with the same length as daily_sales, filled with zeros.
DSA Python
Hint

Use len() to get the length and multiply a list with zero.

3
Fill the prefix sum list with cumulative totals
Use a for loop with variable i from 0 to len(daily_sales) to fill prefix_sum so that each element is the sum of all previous sales including the current day. For i == 0, set prefix_sum[0] to daily_sales[0]. For others, set prefix_sum[i] to prefix_sum[i-1] + daily_sales[i].
DSA Python
Hint

Use range(len(daily_sales)) and an if condition inside the loop.

4
Print the prefix sum list
Print the prefix_sum list to display the cumulative sales totals.
DSA Python
Hint

Use print(prefix_sum) to show the list.