DSA SheetEasy

PREFIX SUMPREFIX SUM

Range Sum Query - Immutable

EasyEditorial · 6 minGenerated by gpt-5.6-luna · Aug 13

Open — the attempt gate is not wired up yet

This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.

Try it yourself first →

Intuitionwhy one extra prefix entry removes the query loop

A range sum from left to right is the sum of everything from index 0 to right, minus the sum of everything before left. If you store those starting-at-zero sums, the elements before left cancel out and only the requested range remains.

Use a prefix array with length n + 1. Define prefix[i] as the sum of nums[0] through nums[i - 1], so prefix[0] is zero and prefix[i + 1] includes nums[i]. With that definition, the inclusive range is prefix[right + 1] - prefix[left]. The extra entry makes the same formula work when left is zero.

A prefix-sum array aligned above an input arrayThe input array is shown in one row with indices 0 through 5 and values -2, 0, 3, -5, 2, and -1. Above it, the prefix array has positions 0 through 6; each position is the sum of input values before that position. For a query from left 2 to right 5, prefix[2] marks the sum before the range and prefix[6] marks the sum through the range's last element. Subtracting prefix[2] from prefix[6] removes the earlier values and leaves nums[2] through nums[5].0-2-21-4-2-3-203-52-1prefix[6] − prefix[2] = nums[2] + nums[3] + nums[4] + nums[5]left = 2right + 1 = 6subtractuse endpointprefixnums0123456012345inclusive range [2..5]
The right endpoint needs right + 1 because the range is inclusive.

Approachbuild once inside the method, then answer by subtraction

  1. Read n and allocate prefix with n + 1 entries initialized to zero, because prefix[0] represents the sum of an empty prefix and lets the formula handle left = 0.
  2. Fill prefix from left to right using prefix[i + 1] = prefix[i] + nums[i], because each entry should include one more input value than the entry before it.
  3. Use right + 1 as the ending prefix position, because prefix[right] stops before nums[right] while the requested range includes it.
  4. Subtract prefix[left] from prefix[right + 1], because prefix[left] contains exactly the values before the requested range and therefore removes them.
  5. Return the difference directly, because every value outside the range appears in both prefix sums and cancels.
  6. Keep the prefix construction before the subtraction, because using an unfilled entry would produce a value that does not represent any input prefix.

Complexitythe query arithmetic is constant time, but this signature rebuilds the table

MEASUREBOUNDWHY
TimeO(n) per callThe loop creates n prefix relationships, and the final subtraction is O(1). Each input element is processed once, so the work grows with the array length rather than with the width of the requested range.
SpaceO(n) extraThe prefix vector stores n + 1 running sums. The returned integer is not additional output storage, and no input shape changes this bound; an array of maximum length still requires a linear prefix table.
Here n is the length of nums. The bounds are for one invocation of sumRange.

Annotated solutionC++ · prefix array · complete judge-ready method

CPPBuild the prefix sums and answer the inclusive range with one subtraction.
#include <vector>
using namespace std;

class Solution {
public:
    int sumRange(vector<int>& nums, int left, int right) {
        int n = nums.size();
        vector<int> prefix(n + 1, 0);

        for (int i = 0; i < n; i++) {
            prefix[i + 1] = prefix[i] + nums[i];
        }

        return prefix[right + 1] - prefix[left];
    }
};

The important placement is the index shift in both the construction and the query. Writing nums[i] into prefix[i + 1] reserves prefix[0] for an empty prefix. The query then ends at right + 1, not right, because the prefix position describes values before its index. These two choices are a matched definition; changing only one creates an off-by-one result.

Common mistakestwo index errors that often look plausible