Range Sum Query Immutable Problem

Range Sum Query Immutable Problem — ExecCode Easy DSA Practice

Solve the Range Sum Query Immutable problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an integer array nums and a list of [L,R] range queries, build a prefix sum table once so that every query sumRange(L,R) — the sum of nums[L..R] inclusive — can be answered in O(1) time without rescanning the array.

Examples

Input {"nums": [-2, 0, 3, -5, 2, -1], "queries": [[0, 2], [2, 5], [0, 5]]}; Output [1, -1, -3]. Input {"nums": [1, 2, 3, 4, 5], "queries": [[0, 4], [1, 3], [2, 2]]}; Output [15, 9, 3]. Input {"nums": [7], "queries": [[0, 0]]}; Output [7]

Constraints

1 ≤ nums.length ≤ 10⁴ -10⁵ ≤ nums[i] ≤ 10⁵ 0 ≤ L ≤ R < nums.length Up to 10⁴ queries after one preprocess

Practice Range Sum Query Immutable free on ExecCode. Browse DSA problems, topic map, and placement guides.