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.
arr
of integers where arr[i]
is the number of gifts delivered on day i
(0-indexed).[start, end]
indicating the range of days (inclusive).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
Precompute a prefix sum array to answer each query in constant time after an O(n) preprocessing step.
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!