Bird
0
0
DSA Cprogramming~5 mins

Prefix Sum Array in DSA C - 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.
Click to reveal answer
beginner
How do you compute the prefix sum array for an input array?
Start with the first element same as original. For each next element, add the current original element to the previous prefix sum element.
Click to reveal answer
intermediate
Why use a Prefix Sum Array?
It helps quickly find the sum of any subarray by subtracting two prefix sums, reducing repeated addition.
Click to reveal answer
beginner
What is the time complexity to build a prefix sum array of size n?
O(n), because you calculate each prefix sum element once by adding the previous prefix sum and current element.
Click to reveal answer
intermediate
How to find the sum of elements between indices i and j using prefix sums?
Sum = prefixSum[j] - prefixSum[i-1] (if i > 0), else prefixSum[j] if i == 0.
Click to reveal answer
What does the element at index 3 in a prefix sum array represent?
ASum of elements from index 0 to 3 in original array
BValue of element at index 3 in original array
CSum of elements from index 3 to end in original array
DDifference between elements at index 3 and 2
What is the time complexity to answer sum queries using prefix sums after building it?
AO(log n)
BO(1)
CO(n)
DO(n^2)
If prefixSum[4] = 15 and prefixSum[1] = 5, what is the sum of elements from index 2 to 4?
A15
B20
C5
D10
Which of these is NOT a benefit of prefix sum arrays?
AFaster subarray sum queries
BSimple to build in O(n)
CLess memory usage than original array
DAvoids repeated addition
What is the prefix sum of the array [2, 4, 6]?
A[2, 6, 12]
B[2, 4, 6]
C[2, 8, 14]
D[0, 2, 6]
Explain how to build a prefix sum array from a given array.
Think of adding one element at a time to the sum of all before it.
You got /3 concepts.
    Describe how prefix sum arrays help in finding the sum of any subarray quickly.
    Subtract sums to get subarray sum.
    You got /3 concepts.