Given an integer array `nums`, handle multiple queries of the following type: - Calculate the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** where `left <= right`. Implement the `NumArray` class: - `NumArray(int[] nums)` Initializes the object with the integer array `nums`. - `int sumRange(int left, int right)` Returns the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** (i.e. `nums[left] + nums[left + 1] + ... + nums[right]`).
Example 1
Input: operations = ["NumArray","sumRange","sumRange","sumRange"], args = [[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]
Output: [null,1,-1,-3]
Explanation: sumRange(0,2) = -2 + 0 + 3 = 1. sumRange(2,5) = 3 - 5 + 2 - 1 = -1. sumRange(0,5) adds up the whole array: -3.
Example 2
Input: operations = ["NumArray","sumRange"], args = [[[1]],[0,0]]
Output: [null,1]
Explanation: The range [0,0] is just the single element 1.
Example 3
Input: operations = ["NumArray","sumRange","sumRange"], args = [[[1,2,3,4]],[1,3],[0,0]]
Output: [null,9,1]
Explanation: sumRange(1,3) = 2 + 3 + 4 = 9 and sumRange(0,0) = 1.
Precompute a prefix-sum array once in the constructor. Any range sum is then the difference of two prefix values, answered in O(1) — perfect since the array never changes.
Run checks all cases above. Submit evaluates all test cases.