Skip to content

Example: Range Sum Query

Roberto Fronteddu edited this page Dec 2, 2024 · 3 revisions

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 such that:

  • 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]).
class NumArray {
    int[] prefixSum;
    public NumArray(int[] nums) {
        int n = nums.length;
        prefixSum = new int[n];
        prefixSum[0] = nums[0];
        for(int i = 1; i < n; i++) {
            prefixSum[i] = prefixSum[i - 1] + nums[i];
        }
    }
    
    public int sumRange(int left, int right) {
        return prefixSum[right] - (left > 0 ? prefixSum[left - 1] : 0)
    }
}
Clone this wiki locally