Holiday Gift Sums

Prefix Sums Arrays Cumulative Sum

Problem: Holiday Gift Sums

During the holiday season, it's important to quickly analyze the gifts delivered each day. You are given an array of integers where each element represents the number of gifts delivered on a particular day. You are also given a list of queries. Each query specifies a range of days, and your task is to compute the total number of gifts delivered within that range.

Input

  • An array arr of integers where arr[i] is the number of gifts delivered on day i (0-indexed).
  • A list of queries where each query is represented as a pair [start, end] indicating the range of days (inclusive).

Output

  • Return an array of integers, where each integer is the sum of gifts delivered for the corresponding query.

Example

Input:
  arr = [5, 2, 9, 3, 1]
  queries = [[1, 3], [0, 2], [2, 4]]

Output:
  [14, 16, 13]

Explanation:
- Query [1, 3]: 2 + 9 + 3 = 14
- Query [0, 2]: 5 + 2 + 9 = 16
- Query [2, 4]: 9 + 3 + 1 = 13

Hint

Precompute a prefix sum array to answer each query in constant time after an O(n) preprocessing step.

Historical Note

As we reflect on the past year during the holiday season, this problem encourages you to look back at the cumulative totals -- much like a year in review. Enjoy the challenge and happy holidays!