Range Sum Query - Mutable

Medium

Description

Given an integer array nums, handle update(index, val) and sumRange(left, right) queries efficiently.

Examples

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

Sum changes after update.

Input:nums = [1], sumRange(0, 2), update(1, 2), sumRange(0, 2)
Output:[1]
Explanation:

Edge case with a single-element array.

Input:nums = [7, -2, 4, 6], sumRange(1, 3), update(0, -5), sumRange(0, 2), update(2, -1), sumRange(1, 3)
Output:[8, -3, 3]
Explanation:

Initial array [7, -2, 4, 6]. First sumRange(1, 3) = -2 + 4 + 6 = 8. After update(0, -5), array becomes [-5, -2, 4, 6]. Then sumRange(0, 2) = -5 + (-2) + 4 = -3. After update(2, -1), array becomes [-5, -2, -1, 6]. Final sumRange(1, 3) = -2 + (-1) + 6 = 3. This example demonstrates handling negative numbers and multiple updates.

Constraints

  • 1 ≤ nums.length ≤ 3 × 10⁴
  • -100 ≤ nums[i] ≤ 100

Ready to solve this problem?

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