Range Sum Query - Immutable - Problem

Imagine you're working with a fixed dataset and need to efficiently answer multiple range sum queries. You're given an integer array nums and must handle numerous queries asking for the sum of elements between specific indices.

Your task: Implement the NumArray class that can:

  • NumArray(int[] nums) - Initialize with the integer array
  • int sumRange(int left, int right) - Return the sum of elements from index left to right (inclusive)

Key Challenge: Since there will be many queries, you need to optimize for query time rather than just solving it once. A naive approach of summing elements for each query would be too slow for large datasets with frequent queries.

Example: Given nums = [-2, 0, 3, -5, 2, -1]
โ€ข sumRange(0, 2) returns 1 (sum of -2, 0, 3)
โ€ข sumRange(2, 5) returns -1 (sum of 3, -5, 2, -1)

Input & Output

example_1.py โ€” Basic Range Queries
$ Input: nums = [-2, 0, 3, -5, 2, -1]\nQueries: sumRange(0, 2), sumRange(2, 5), sumRange(0, 5)
โ€บ Output: 1, -1, -3
๐Ÿ’ก Note: sumRange(0, 2) = -2 + 0 + 3 = 1. sumRange(2, 5) = 3 + (-5) + 2 + (-1) = -1. sumRange(0, 5) = sum of entire array = -3
example_2.py โ€” Single Element Range
$ Input: nums = [1, 2, 3, 4, 5]\nQueries: sumRange(2, 2), sumRange(0, 0)
โ€บ Output: 3, 1
๐Ÿ’ก Note: When left equals right, we return the single element at that index. sumRange(2, 2) returns nums[2] = 3, sumRange(0, 0) returns nums[0] = 1
example_3.py โ€” Full Array Range
$ Input: nums = [5, -3, 5]\nQueries: sumRange(0, 2)
โ€บ Output: 7
๐Ÿ’ก Note: sumRange(0, 2) covers the entire array: 5 + (-3) + 5 = 7. This tests the boundary case where we query the complete range

Visualization

Tap to expand
๐Ÿ“š Library Ledger SystemShelf Values:$50$30$70$40$60$20Shelf 1Shelf 2Shelf 3Shelf 4Shelf 5Shelf 6Running Total Ledger:$0Start$50To Shelf 1$80To Shelf 2$150To Shelf 3$190To Shelf 4$250To Shelf 5$270To Shelf 6Query: Shelf 3 to 5Answer: $250 - $80 = $170 (Instant!)๐Ÿ’ก Key Insight: Preprocessing enables instant range queries!
Understanding the Visualization
1
Initial Setup
Create a ledger where each entry shows total value from first shelf to current position
2
Query Arrives
Customer asks: 'What's the total value from shelf 3 to shelf 7?'
3
Quick Lookup
Look up total-to-shelf-7 minus total-to-shelf-2 in the ledger
4
Instant Answer
Provide answer immediately without walking through shelves
Key Takeaway
๐ŸŽฏ Key Insight: Prefix sums transform multiple expensive O(n) range queries into instant O(1) calculations through smart preprocessing

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1) per query, O(n) initialization

After O(n) preprocessing, each query is answered in constant time using simple subtraction

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Additional array to store prefix sums, same size as input array

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • -105 โ‰ค nums[i] โ‰ค 105
  • 0 โ‰ค left โ‰ค right < nums.length
  • At most 104 calls will be made to sumRange
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.1K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen