0
0
DSA Pythonprogramming~5 mins

Prefix Sum Array in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Prefix Sum Array?
A Prefix Sum Array is a new array where each element at index i is the sum of all elements from the start up to i in the original array. It helps to quickly find the sum of any subarray.
Click to reveal answer
beginner
How do you calculate the prefix sum array for [2, 4, 6, 8]?
Start with the first element: 2. Then add each next element to the sum so far: [2, 2+4=6, 6+6=12, 12+8=20]. So the prefix sum array is [2, 6, 12, 20].
Click to reveal answer
beginner
Why is a prefix sum array useful?
It allows you to find the sum of any part of the array quickly by subtracting two prefix sums instead of adding many elements each time.
Click to reveal answer
intermediate
What is the time complexity to build a prefix sum array of size n?
The time complexity is O(n) because you calculate each prefix sum by adding one element to the previous sum in a single pass.
Click to reveal answer
intermediate
How do you find the sum of elements from index i to j using a prefix sum array?
Sum from i to j = prefix_sum[j] - prefix_sum[i-1] if i > 0, else prefix_sum[j] if i == 0.
Click to reveal answer
What does the element at index 3 in a prefix sum array represent?
ADifference between elements at index 3 and 2
BValue of the original array at index 3
CSum of elements from index 3 to end of the array
DSum of elements from index 0 to 3 in the original array
What is the time complexity to find the sum of any subarray using a prefix sum array?
AO(1)
BO(log n)
CO(n)
DO(n^2)
If prefix_sum = [3, 7, 12, 18], what is the sum of elements from index 1 to 3?
A18
B15
C12
D9
Which of the following is NOT true about prefix sum arrays?
AThey store the product of elements up to index i
BThey require O(n) time to build
CThey help in quick range sum queries
DThey can be used to optimize sum calculations
What is the prefix sum array of [1, 1, 1, 1]?
A[4, 3, 2, 1]
B[1, 1, 1, 1]
C[1, 2, 3, 4]
D[0, 1, 2, 3]
Explain how to build a prefix sum array from a given array and why it is useful.
Think about adding elements one by one and how it helps in fast queries.
You got /3 concepts.
    Describe how to find the sum of elements between two indices i and j using a prefix sum array.
    Use subtraction of prefix sums to get the subarray sum.
    You got /3 concepts.