Prefix Sum Queries

Prefix Sums Arrays Queries Easy Medium

Problem: Prefix Sum Queries

Given an array of integers and a list of queries, compute the sum of the subarray defined by each query. Each query is represented by two indices [start, end] which specify the beginning and ending positions (inclusive) of the subarray. Your task is to implement a function that efficiently computes these sums using a prefix sums approach.

Example

For the array:

[2, 4, 6, 8, 10]

And queries:

[[1, 3], [0, 4], [2, 2]]

The output should be:

[18, 30, 6]

Hint:

  • First, compute the prefix sum array where each element at index i represents the sum of the array from index 0 to i.
  • Use the prefix sum array to quickly compute the sum for any given subarray.

Historical Note:

February 17 has seen many notable events in the history of science and mathematics. Today, as you solve this problem, you can appreciate how mathematical ideas like prefix sums have been key tools in developing efficient algorithms.