Compute the sum of each query subarray, where the subarray length may vary from query to query.
Given an array of integers, answer multiple queries of the form: sum the elements in a subarray whose length is specified by the query. For each query, use the starting position and requested length to determine the segment, then return the sum of that segment.
This problem is a good fit for prefix sums because it asks for repeated range-sum queries on a static array.
nums.Example 1
Input
nums = [1, 2, 3, 4] queries = [[0, 2], [1, 3], [2, 1]]
Output
[3, 9, 3]
Explanation
The queried subarrays are [1, 2], [2, 3, 4], and [3]. Their sums are 3, 9, and 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.