Prefix Sum is a method for dealing with array manipulation challenges. It entails establishing an auxiliary array in which each element keeps the sum of all the components preceding it(including the current element). A prefix sum array is another name for this auxiliary array.
Sample example to understand the concept:
Input: arr[] = {10, 15, 20, 25, 30}
Output: prefixSum[] = {10, 25, 45, 70, 100}
Explanation: While traversing the array, update the element by adding it to its previous element.
prefixSum[0] = 10,
prefixSum[1] = prefixSum[0] + arr[1] = 25,
prefixSum[2] = prefixSum[1] + arr[2] = 45 and so on.
Here’s a step-by-step explanation of the approach:
prefixSum[]
of the same size as the input array.prefixSum[]
array to the value of the first element of the input array.prefixSum[]
array.prefixSum[]
array.prefixSum[]
array.Here’s an example implementation of the above approach in Java:
public static int[] prefixSum(int[] arr) {
int n = arr.length;
int[] prefixSum = new int[n];
prefixSum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefixSum[i] = prefixSum[i-1] + arr[i];
}
return prefixSum;
}
This function takes an input array arr
and returns its prefix sum array prefixSum
. The time complexity of this algorithm is O(n), where n is the size of the input array.
Once we’ve computed the prefix sum array for the input array, you can quickly compute the sum of any subarray between indices l and r in constant time using the prefix sum array.
Note: The brute force method is to find the sum using the for loop, which costs O(n) time per query. While the efficient method uses the prefixSum array to answer each query in constant time.
The steps required to find are –
sum[l, r]
, do the following:l
is equal to 0
, then the sum of the subarray between indices l
and r
is simply the value of the prefix sum at index r
. So, return prefixSum[r]
.l
and r
can be calculated as the difference between the value of the prefix sum at index r
and the value of the prefix sum at index l-1
. So, return prefixSum[r] - prefixSum[l-1]
.Here’s an example implementation of the above approach in Java:
public static int rangeSumQuery(int l, int r, int[] prefixSum) {
if (l == 0) {
return prefixSum[r];
}
return prefixSum[r] - prefixSum[l-1];
}
This function takes the indices l
and r
for the range sum query and the prefix sum array prefixSum
as input, and returns the sum of the subarray between indices l
and r
. The time complexity of this algorithm is O(1), which means that it can compute the sum of any subarray in constant time.
Note: also read about DSA: Concept of Array
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…