Back to DSA sheet

Range Sum Query - Immutable

EasyArrays

Given a fixed array, answer several queries each asking for the sum of nums[i..j] inclusive. Precompute so each query is O(1).

Input format (stdin): line 1 has n; line 2 has n integers; line 3 has q; each of the next q lines has i and j. Output each query's sum on its own line.

Examples
Input: 6 -2 0 3 -5 2 -1 3 0 2 2 5 0 5
Output: 1 -1 -3
sum(0,2)=1, sum(2,5)=-1, sum(0,5)=-3.
Constraints
  • 1 <= n <= 10^4
  • 0 <= i <= j < n
  • 1 <= q <= 10^4
Sheets
NeetCode 250
range-sum-query-immutable.cpp1 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.