Range Sum Query - Immutable

EasyArraySQL

Description

Given an integer array nums, handle multiple queries sumRange(left, right) which returns the sum of elements between indices left and right inclusive. Return the result as an integer.

Examples

Input:nums = [-2, 0, 3, -5, 2, -1], sumRange(0, 2)
Output:1
Explanation:

Using prefix sums for O(1) queries: sumRange(0,2) computes prefix[3] - prefix[0] = 1 - 0 = 1, which equals -2 + 0 + 3.

Input:nums = [-2, 0, 3, -5, 2, -1], arg2 = 0, arg3 = 2
Output:1
Explanation:

Sum of elements from index 0 to 2: -2 + 0 + 3 = 1.

Input:nums = [-2, 0, 3, -5, 2, -1], arg2 = 2, arg3 = 5
Output:-1
Explanation:

Sum of elements from index 2 to 5: 3 + (-5) + 2 + (-1) = -1.

Constraints

  • 1 ≤ nums.length ≤ 10⁴
  • -10⁵ ≤ nums[i] ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!