Range Sum Query - Immutable
Try to solve the Range Sum Query - Immutable problem.
We'll cover the following
Statement
You are given an integer array, nums
, and you need to handle multiple queries of the following type:
Query: Calculate the sum of elements in
nums
between indicesi
andj
(inclusive), wherei <= j
.
Implement the NumArray
class to support the following operations efficiently:
Constructor
(NumArray(int[] nums)
: Initializes the object with the integer arraynums
.int sum_range(int i, int j)
: Returns the sum of the elements ofnums
between indicesi
andj
(inclusive), i.e., the sum ofnums[i] + nums[i + 1] + ... + nums[j]
.
Constraints:
nums.length
nums[i]
i
j
nums.length
At most,
calls will be made to sum_range
.
Examples
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.