📖 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, 2
Create an integer array called prefix_sum with the same length as sales
Create an integer variable called n and set it to the length of sales
Use a for loop with variable i from 0 to n - 1 to fill prefix_sum
Print 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
1
Create the sales data array
Create an integer array called sales with exactly these values: 5, 3, 8, 6, 2.
DSA C
Hint
Use curly braces to list the values inside the array.
2
Create prefix sum array and length variable
Create an integer array called prefix_sum with length 5 and an integer variable called n set to 5.
DSA C
Hint
Declare prefix_sum as an integer array of size 5 and n as an integer with value 5.
3
Fill the prefix sum array
Use a for loop with variable i from 0 to n - 1 to fill prefix_sum so that each element is the sum of all sales elements up to index i. For i == 0, set prefix_sum[0] to sales[0]. For other i, set prefix_sum[i] to prefix_sum[i - 1] + sales[i].
DSA C
Hint
Use an if inside the loop to handle the first element separately.
4
Print the prefix sum array
Use a for loop with variable i from 0 to n - 1 to print each element of prefix_sum followed by a space. Then print a newline character.
DSA C
Hint
Use printf("%d ", prefix_sum[i]) inside the loop and printf("\n") after the loop.
Practice
(1/5)
1. What is the main purpose of a prefix sum array in data structures?
easy
A. To quickly find the sum of elements in any subarray
B. To sort the array elements in ascending order
C. To store the product of all elements
D. To reverse the array efficiently
Solution
Step 1: Understand prefix sum array definition
A prefix sum array stores sums of elements from the start up to each index.
Step 2: Identify its use
This allows quick calculation of sums for any subarray by subtracting prefix sums.
Final Answer:
To quickly find the sum of elements in any subarray -> Option A
Quick Check:
Prefix sum = fast subarray sums [OK]
Hint: Prefix sums help find subarray sums fast [OK]
Common Mistakes:
Confusing prefix sum with sorting
Thinking it stores products
Assuming it reverses arrays
2. Which of the following is the correct way to initialize a prefix sum array prefix for an integer array arr of size n in C?
Hint: Add current element to previous prefix sum [OK]
Common Mistakes:
Confusing prefix sums with original array
Starting sums from zero incorrectly
Adding wrong elements in loop
4. Identify the error in the following code snippet that attempts to create a prefix sum array for arr of size n:
int prefix[n];
prefix[0] = 0;
for(int i = 1; i < n; i++) {
prefix[i] = prefix[i-1] + arr[i];
}
medium
A. prefix[0] should be initialized to arr[0], not 0
B. Loop should start from i = 0, not 1
C. Array prefix should be of size n+1
D. The code will cause a runtime error
Solution
Step 1: Check prefix[0] initialization
prefix[0] must hold arr[0] to start sums correctly, not zero.
Step 2: Analyze loop correctness
Loop starting at 1 is correct if prefix[0] is set properly.
Final Answer:
prefix[0] should be initialized to arr[0], not 0 -> Option A
Quick Check:
prefix[0] = arr[0] starts sums right [OK]
Hint: Initialize prefix[0] with arr[0], not zero [OK]
Common Mistakes:
Setting prefix[0] to zero causes wrong sums
Starting loop at wrong index
Assuming prefix array size must be n+1
5. You have an array arr = {3, 1, 4, 1, 5, 9, 2}. Using a prefix sum array, how would you efficiently find the sum of elements from index 2 to 5 (inclusive)?
hard
A. Calculate prefix[5] + prefix[2]
B. Sum elements from arr[2] to arr[5] directly every time
C. Use prefix[6] - prefix[2] to get the sum
D. Calculate prefix[5] - prefix[1] after building prefix sums
Solution
Step 1: Build prefix sum array
prefix[i] = sum of arr[0] to arr[i]. For example, prefix[5] = sum of arr[0..5].
Step 2: Calculate sum from index 2 to 5
Sum = prefix[5] - prefix[1], because prefix[1] = sum of arr[0..1].
Final Answer:
Calculate prefix[5] - prefix[1] after building prefix sums -> Option D
Quick Check:
Sum(i..j) = prefix[j] - prefix[i-1] [OK]
Hint: Sum subarray = prefix[end] - prefix[start-1] [OK]
Common Mistakes:
Adding prefix sums instead of subtracting
Using wrong indices for prefix subtraction
Summing elements directly ignoring prefix sums
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.