Range boundaries · Easy
A report requests the total of one selected range in an immutable numeric array. Only one range is evaluated per call, so building a reusable index is not required. The selection includes both endpoints and signed adjustments must remain signed.
Return the sum of nums[left] through nums[right] inclusive without including values outside that range.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[], left: integer, right: integer }
Fields
- nums is a non-empty array of signed integers. left and right are inclusive zero-based indexes.
- This is one query per call, not an online stream of range queries. Inputs satisfy 0 <= left <= right < nums.length.
Output
Return value
- Return one integer sum, not the selected values. A one-element range returns that element.
- Use a 64-bit integer in Java/C++; sums may exceed 32-bit range. Zero and negative totals are valid.
Constraints
- 1 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
- Do not mutate input. The result fits a safe JavaScript integer and a signed 64-bit integer.
Examples
Example 1
{
"left": 1,
"nums": [
7,
-3,
2,
9
],
"right": 2
}Returns
-1Example 2
{
"left": 0,
"nums": [
-7
],
"right": 0
}Returns
-7Required complexity
O(right-left+1) solution time and O(1) solution space excluding the supplied input and adapter