Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Range Sum Query - Immutable

Easy

Topics
ArrayPrefix SumDesign

Given an integer array nums, handle multiple queries of the sum of the elements between indices left and right inclusive. Implement NumArray(nums) and sumRange(left, right) which returns the sum nums[left] + ... + nums[right].

Example 1

Input:  operations = ["NumArray","sumRange","sumRange","sumRange"], values = [[-2,0,3,-5,2,-1],[0,2],[2,5],[0,5]]
Output: [null,1,-1,-3]

Constraints

  • 1 <= nums.length <= 10^4
  • -10^5 <= nums[i] <= 10^5
  • 0 <= left <= right < nums.length
  • At most 10^4 calls to sumRange.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access