📖 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.
🎯 Goal: Build a prefix sum array from the daily sales array. This array will help you find the sum of sales from the start up to any day efficiently.
📋 What You'll Learn
Create an integer array called sales with the exact values: 5, 3, 8, 6, 2
Create an integer array called prefixSum with the same length as sales
Fill prefixSum so that each element at index i is the sum of sales[0] to sales[i]
Print the prefixSum array elements separated by spaces
💡 Why This Matters
🌍 Real World
Prefix sum arrays are used in finance to quickly calculate cumulative totals, in gaming for score tracking, and in data analysis for fast range queries.
💼 Career
Understanding prefix sums helps in optimizing algorithms for performance, a key skill in software development and data engineering roles.
Progress0 / 4 steps
1
Create the sales array
Create an integer array called sales with these exact values: 5, 3, 8, 6, 2
DSA Java
Hint
Use int[] sales = {5, 3, 8, 6, 2}; to create the array.
2
Create the prefixSum array
Create an integer array called prefixSum with the same length as sales
DSA Java
Hint
Use new int[sales.length] to create the array.
3
Fill the prefixSum array with sums
Fill the prefixSum array so that each element at index i is the sum of sales[0] to sales[i]. Use a for loop with variable i from 0 to sales.length - 1. For i == 0, set prefixSum[0] to sales[0]. For other i, set prefixSum[i] to prefixSum[i - 1] + sales[i].
DSA Java
Hint
Use a for loop and an if statement to fill the prefixSum array.
4
Print the prefixSum 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 System.out.print(prefixSum[i] + " ") inside the loop and System.out.println() after the loop.
DSA Java
Hint
Use a for loop and System.out.print to print each element followed by a space.
Practice
(1/5)
1. What does a prefix sum array represent for a given array arr?
easy
A. An array where each element is the sum of all elements up to that index in arr
B. An array where each element is the product of all elements up to that index in arr
C. An array where each element is the difference between consecutive elements in arr
D. An array where each element is the maximum value found up to that index in arr
Solution
Step 1: Understand prefix sum definition
A prefix sum array stores sums of elements from the start of the array up to each index.
Step 2: Compare options with definition
Only An array where each element is the sum of all elements up to that index in arr matches this definition exactly.
Final Answer:
An array where each element is the sum of all elements up to that index in arr -> Option A
Quick Check:
Prefix sum = cumulative sum [OK]
Hint: Prefix sum means cumulative sum from start to current index [OK]
Common Mistakes:
Confusing prefix sum with product or max operations
Thinking prefix sum stores differences
Mixing prefix sum with suffix sum
2. Which of the following Java code snippets correctly initializes a prefix sum array prefix for an integer array arr?
easy
A. int[] prefix = new int[arr.length]; for(int i=0; i
B. int[] prefix = new int[arr.length]; prefix[0] = 0; for(int i=1; i
C. int[] prefix = new int[arr.length]; prefix[0] = arr[0]; for(int i=1; i
D. int[] prefix = new int[arr.length]; for(int i=0; i
Solution
Step 1: Check initialization of prefix array
Prefix sum array must start with prefix[0] = arr[0] to include first element sum.
Step 2: Verify loop calculation
Loop from i=1 to end adds previous prefix sum plus current arr element, which is correct.
Final Answer:
int[] prefix = new int[arr.length]; prefix[0] = arr[0]; for(int i=1; i -> Option C
Quick Check:
Initialize first element, then add previous sums [OK]
Hint: Start prefix[0] with arr[0], then add previous sums in loop [OK]
Hint: Add current element to previous prefix sum for each index [OK]
Common Mistakes:
Printing original array instead of prefix sum
Starting prefix sum with 0 incorrectly
Adding wrong elements in loop
4. Identify the error in the following Java code that attempts to create a prefix sum array:
int[] arr = {1, 3, 5};
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 condition should be i < arr.length instead of i <= arr.length
B. prefix[0] should be initialized to arr[0] instead of 0
C. Array index arr[i] will cause an error when i equals arr.length
D. All of the above
Solution
Step 1: Check loop boundary
The loop uses i <= arr.length, which causes index out of bounds because valid indices are 0 to arr.length-1.
Step 2: Check prefix[0] initialization and array access
prefix[0] should be arr[0] to start sums correctly; also arr[i] access fails at i = arr.length.
Step 3: Combine all errors
All issues cause runtime errors or incorrect prefix sums.
Final Answer:
All of the above -> Option D
Quick Check:
Loop bounds, initialization, and indexing errors combined [OK]
Hint: Check loop limits and array indices carefully [OK]
Common Mistakes:
Using <= instead of < in loop condition
Initializing prefix[0] incorrectly
Accessing array out of bounds
5. Given an integer array arr of length 5: {3, 1, 4, 1, 5}, you want to find the sum of elements from index 1 to 3 (inclusive) efficiently using a prefix sum array. Which expression correctly calculates this sum using the prefix sum array prefix where prefix[i] is the sum of elements from index 0 to i?
hard
A. prefix[3] - prefix[2]
B. prefix[3] - prefix[0]
C. prefix[3] - prefix[1]
D. prefix[3] - prefix[0] + arr[1]
Solution
Step 1: Understand prefix sum usage for range sums
Sum from index l to r = prefix[r] - prefix[l-1]. Here l=1, r=3.
Step 2: Apply formula with given indices
Sum = prefix[3] - prefix[0]. prefix[0] is sum up to index 0, so subtracting it leaves sum from 1 to 3.
Final Answer:
prefix[3] - prefix[0] -> Option B
Quick Check:
Range sum = prefix[r] - prefix[l-1] [OK]
Hint: Range sum = prefix[end] - prefix[start - 1] [OK]
Common Mistakes:
Subtracting prefix at wrong index
Adding extra elements unnecessarily
Confusing inclusive indices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.