Range Sum Query Immutable
To solve this coding challenge, we can utilize a technique known as prefix sum, which allows us to preprocess the given array in such a way that it facilitates efficient querying of subarray sums. The prefix sum array stores cumulative sums of the elements from the beginning of the array up to each index. This way, any subarray sum can be quickly computed using the difference between two prefix sums.
Explanation
Concept
- Prefix Sum Array :
-
Create a prefix sum array where each element at index
represents the sum of all elements from the beginning of the array up to index
i.i-1 -
For example, if the input array is
, the prefix sum array will be
[a, b, c, d].[0, a, a+b, a+b+c, a+b+c+d] - Querying Sum :
-
To find the sum of elements between indices
and
left, compute the difference between the prefix sums atrightandright+1.left -
This works because the prefix sum at
includes the sum of all elements up to
right+1, and the prefix sum atrightincludes the sum of all elements up toleft. Hence, the difference gives the sum of elements in the rangeleft-1.[left, right] - Initialization :
-
Initialize an empty list called
.
prefix_sum -
Set the first element of
to
prefix_sumsince there is no element before the start of the array.0 - Building Prefix Sum Array :
- Iterate through each element of the input array.
-
Append the sum of the current element and the last element of the
to the
prefix_sumlist.prefix_sum -
Query Function (
) :
sumRange -
For each query, return the difference between the elements at indices
and
right + 1in theleftarray.prefix_sum
Steps in Pseudocode
Step-by-Step Explanation
Detailed Steps in Pseudocode
# Class definition for NumArray
class NumArray:
# Constructor to initialize the object with the given integer array
method __init__(nums):
# Initialize an empty prefix_sum list with an initial value of 0
prefix_sum = [0]
# Loop through each element in the given nums array
for each num in nums:
# Append the current prefix sum + current number to the prefix_sum list
prefix_sum.append(prefix_sum[-1] + num)
# Method to calculate the sum of elements between indices left and right (inclusive)
method sumRange(left, right):
# Return the difference between prefix_sum at index right+1 and prefix_sum at index left
return prefix_sum[right + 1] - prefix_sum[left]
Explanation
Initialization
-
The
method receives the integer array
__init__and constructs thenumsarray.prefix_sum -
By starting the
array with 0, it simplifies the calculation of prefix sums, as the initial value represents an empty sum.
prefix_sum
Building Prefix Sum Array
-
For each element in
, add its value to the last element of
numsand append this new sum toprefix_sum.prefix_sum -
This continuous accumulation ensures that each subsequent index in
represents the cumulative sum of elements of
prefix_sumup to that point.nums
Query Function (`sumRange`)
-
The
method takes two arguments,
sumRangeandleft, representing the indices of the subarray whose sum we need.right -
By calculating the difference
, it gives the sum of the elements from
prefix_sum[right + 1] - prefix_sum[left]toleft, inclusive.right -
This difference works because
includes all elements up to
prefix_sum[right + 1], and subtractingrightremoves the elements beforeprefix_sum[left].left