2Brute2Best
🔥0

Range Sum Query – Immutable

Easy·LeetCode #303 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given an integer array nums that never changes. After that, you will be asked many questions of the form "what is the sum of nums[left] through nums[right], both ends included?"

Write a class NumArray:

  • NumArray(vector<int>& nums) receives the array once.
  • int sumRange(int left, int right) returns nums[left] + nums[left + 1] + … + nums[right].
Input

The array nums (given once, to the constructor), followed by many calls sumRange(left, right).

Output

For each sumRange call, the sum of the elements from index left to index right, inclusive.

Example 1
Input: nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2), sumRange(2, 5), sumRange(0, 5)
Output: 1, -1, -3

(-2) + 0 + 3 = 1. 3 + (-5) + 2 + (-1) = -1. The whole array adds up to -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
  • • Every total is at most 10^4 · 10^5 = 10^9 in size, so `int` is enough.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“array never changes”
→ Totals computed once stay correct forever → precompute.
“many queries”
→ Spend O(N) once so each query is O(1).
“sum from left to right”
→ A range sum = difference of two totals from the start.
03

Approach Complexity Ladder

Brute Force — loop over the range per queryO(N)O(N)

Keep the array; each query adds nums[left..right] one by one

Optimal — prefix array with a leading 0O(1)O(N)

Build prefix of size n+1 once; each query is prefix[r+1] − prefix[l]

Brute Force Intuition

Store the array as it is. For each query, loop from left to right and add up the elements.

Recognition rule for this sub-pattern:

Many range-sum questions on data that never changes. Build prefix once, answer each query in O(1).
Remember this · Prefix Sum
Trigger
Range sums, "subarray sum equals K" (especially with negatives), equal counts, divisible by K.
Think
If I knew the total up to every index, would the answer become a difference of two totals?
Key question
Which earlier prefix value am I looking for when I stand at index i?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.