P3 · 2 seconds · 256 MB

Range Sum

You are given an integer array `nums`. Implement `rangeSum(nums, queries)` where `queries` is an array of `[l, r]` pairs (inclusive, 0-based). Return an array of answers — the sum of nums[l..r] for each query. Precompute so that you are not looping the range per query.

function rangeSum(nums, queries) → number[]

CONSTRAINTS

  • 1 ≤ nums.length ≤ 10⁴
  • 1 ≤ queries.length ≤ 10⁴
  • 0 ≤ l ≤ r < nums.length
  • -10⁵ ≤ nums[i] ≤ 10⁵
Input: nums = [2, 1, 3, 4], queries = [[0, 2], [1, 3]]
Output: [6, 8]
2+1+3 = 6, 1+3+4 = 8
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.