📖 Scenario: You are working with a list of daily sales numbers for a small shop. To quickly find the total sales between any two days, you want to create a prefix sum array. This array will help you add up sales faster without adding each day every time.
🎯 Goal: Build a prefix sum array from the daily sales list. The prefix sum array stores the total sales from day 0 up to each day. Then print the prefix sum array to see the cumulative sales.
📋 What You'll Learn
Create an integer array called dailySales with these exact values: 3, 5, 2, 8, 6
Create an integer array called prefixSum with the same length as dailySales
Fill prefixSum so that each element at index i is the sum of dailySales[0] to dailySales[i]
Print the prefixSum array elements separated by spaces
💡 Why This Matters
🌍 Real World
Prefix sum arrays are used in finance, data analysis, and gaming to quickly calculate sums over ranges without repeated addition.
💼 Career
Understanding prefix sums helps in coding interviews and real-world tasks where performance matters for large data sets.
Progress0 / 4 steps
1
Create the daily sales array
Create an integer array called dailySales with these exact values: 3, 5, 2, 8, 6
DSA C#
Hint
Use int[] dailySales = {3, 5, 2, 8, 6}; to create the array.
2
Create the prefix sum array
Create an integer array called prefixSum with the same length as dailySales
DSA C#
Hint
Use new int[dailySales.Length] to create the prefixSum array.
3
Fill the prefix sum array
Fill the prefixSum array so that each element at index i is the sum of dailySales[0] to dailySales[i]. Use a for loop with variable i from 1 to dailySales.Length - 1. Set prefixSum[0] to dailySales[0] before the loop.
DSA C#
Hint
Remember the first prefix sum is the first daily sale. Then add each daily sale to the previous prefix sum.
4
Print the prefix sum array
Print the elements of the prefixSum array separated by spaces using a for loop with variable i from 0 to prefixSum.Length - 1. Use Console.Write for each element and a space. After the loop, print a new line with Console.WriteLine().
DSA C#
Hint
Use a loop to print each prefix sum followed by a space, then print a new line.
Practice
(1/5)
1. What is the main purpose of a Prefix Sum Array in algorithms?
easy
A. To quickly find the sum of elements in any subarray
B. To sort the array elements in ascending order
C. To find the maximum element in the array
D. To reverse the array elements
Solution
Step 1: Understand what prefix sums represent
A prefix sum array stores cumulative sums of elements from the start up to each index.
Step 2: Identify the use of prefix sums
Using prefix sums, we can find the sum of any subarray quickly by subtracting two prefix sums.
Final Answer:
To quickly find the sum of elements in any subarray -> Option A
Quick Check:
Prefix Sum Array = Fast subarray sums [OK]
Hint: Prefix sums help sum subarrays fast by subtraction [OK]
Common Mistakes:
Thinking prefix sums sort the array
Confusing prefix sums with maximum element search
Assuming prefix sums reverse the array
2. Which of the following C# code snippets correctly initializes a prefix sum array prefix from an integer array arr?
easy
A. int[] prefix = new int[arr.Length+1]; for(int i=1; i<=arr.Length; i++) prefix[i] = prefix[i-1] + arr[i];
B. int[] prefix = new int[arr.Length]; for(int i=0; i
C. int[] prefix = new int[arr.Length]; prefix[0] = arr[0]; for(int i=1; i
D. int[] prefix = new int[arr.Length]; prefix[0] = 0; for(int i=1; i
Solution
Step 1: Check initialization of prefix array
int[] prefix = new int[arr.Length]; prefix[0] = arr[0]; for(int i=1; i
Step 2: Verify loop for cumulative sum
The loop starts from 1 and adds prefix[i-1] + arr[i], correctly building prefix sums.
Final Answer:
int[] prefix = new int[arr.Length]; prefix[0] = arr[0]; for(int i=1; i -> Option C
Quick Check:
Correct prefix sum initialization = int[] prefix = new int[arr.Length]; prefix[0] = arr[0]; for(int i=1; i
Hint: Start prefix[0] = arr[0], then add previous prefix + current element [OK]
Common Mistakes:
Using wrong array length for prefix
Starting loop from 0 instead of 1
Accessing arr out of bounds (like arr[i] when i == arr.Length)
3. Given the array arr = new int[] {2, 4, 6, 8} and its prefix sum array prefix computed as usual, what is the output of prefix[3] - prefix[1]?
Hint: Subtract prefix sums to get subarray sum [OK]
Common Mistakes:
Confusing indices and subtracting wrong prefix values
Adding prefix sums instead of subtracting
Forgetting prefix sums start at index 0
4. Consider the following code snippet to build a prefix sum array. Identify the error:
int[] prefix = new int[arr.Length];
prefix[0] = 0;
for(int i = 1; i < arr.Length; i++) {
prefix[i] = prefix[i-1] + arr[i];
}
medium
A. Loop should start from i = 0, not 1
B. prefix[0] should be arr[0], not 0
C. Array prefix should have length arr.Length + 1
D. No error, code is correct
Solution
Step 1: Check initialization of prefix[0]
Setting prefix[0] = 0 is incorrect; it should be arr[0] to start cumulative sum correctly.
Step 2: Verify loop and array length
The loop starting at 1 and array length are fine; only prefix[0] initialization is wrong.
Final Answer:
prefix[0] should be arr[0], not 0 -> Option B
Quick Check:
Correct prefix start = arr[0] [OK]
Hint: Initialize prefix[0] with arr[0], not zero [OK]
Common Mistakes:
Starting loop at 0 causing index errors
Using wrong prefix array size
Assuming zero initialization is always correct
5. You have an integer array arr of length 6: {3, 1, 4, 1, 5, 9}. You want to find the sum of elements from index 2 to 5 inclusive using a prefix sum array prefix where prefix[i] stores sum of elements from index 0 to i. Which expression correctly computes this sum?
hard
A. prefix[5] - prefix[1]
B. prefix[5] - prefix[2]
C. prefix[4] - prefix[1]
D. prefix[5] + prefix[1]
Solution
Step 1: Understand prefix sum indexing
prefix[i] = sum of arr[0] to arr[i]. To get sum from index 2 to 5, subtract prefix[1] (sum up to index 1) from prefix[5].
Step 2: Write the correct expression
Sum = prefix[5] - prefix[1]
Final Answer:
prefix[5] - prefix[1] -> Option A
Quick Check:
Sum(2 to 5) = prefix[5] - prefix[1] [OK]
Hint: Sum(i to j) = prefix[j] - prefix[i-1] [OK]
Common Mistakes:
Subtracting prefix[i] instead of prefix[i-1]
Adding prefix sums instead of subtracting
Using wrong indices for prefix array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.