Skip to content
Forge Learn/Array & String Techniques

Prefix Sums & In-Place Array Tricks

7 min read

You'll learn to

  • -Precompute prefix sums to answer range-sum queries in O(1) each
  • -Reverse or rotate an array without allocating extra space
  • -Recognize when an O(n) preprocessing step pays for itself across many queries

This final chapter before the tier's boss level pulls together two more techniques worth having ready, both built on ideas that have already recurred across this tier: precomputation, and true in-place mutation.

Prefix Sums: Trade O(n) Preprocessing for O(1) Queries

Given a fixed array and MANY range-sum queries over it ("what is the sum from index i to j?"), recomputing sum(items[i:j+1]) for every single query costs O(n) per query - reasonable once, expensive across many repeated queries. A prefix sum array fixes this by precomputing, once, a running total where prefix[i] holds the sum of every element up to and including index i - 1, with prefix[0] = 0 by convention. Any inclusive range sum from index i to index j then becomes a single subtraction: prefix[j + 1] - prefix[i].

Precompute once, then answer any range-sum query in O(1)

For items = [2, 4, 6, 8, 10], the prefix array is [0, 2, 6, 12, 20, 30]. Calling range_sum(prefix, 1, 3) asks for items[1] + items[2] + items[3], which is 4 + 6 + 8 = 18, and prefix[4] - prefix[1] = 20 - 2 = 18 confirms the match - computed in O(1), regardless of how wide the requested range is.

Range-sum query cost comparison
O(n) per query
Recompute each range sum directly
O(1) per query
Prefix sums (after O(n) one-time build)

This is the same "pay once up front, save on every repeated question afterward" trade already seen twice this tier - a hashmap's O(1) lookups after building the table, and memoization's cached subproblem answers.

In-Place Array Tricks

In-place means transforming an array using only the O(1) extra space of a handful of index or temporary variables, with no second array allocated - the same discipline behind the in-place reversal earlier in this module. Rotating an array by k positions is a good second example: rather than building a brand-new rotated list, a classic in-place approach reverses the whole array, then reverses each of the two resulting pieces separately.

Rotating an array with three in-place reversals instead of a new list

Tracing rotate_in_place([1, 2, 3, 4, 5, 6, 7], 3): reversing everything gives [7, 6, 5, 4, 3, 2, 1]. Reversing the first k = 3 elements gives [5, 6, 7, 4, 3, 2, 1]. Reversing the remaining elements from index 3 through 6 gives [5, 6, 7, 1, 2, 3, 4] - exactly the original array rotated right by 3, produced with three O(n) reversal passes and zero extra arrays allocated.

Array rotation cost comparison
O(n) time, O(n) space
Build a new rotated list with slicing
O(n) time, O(1) space
Triple in-place reversal
Check Yourself1 / 3

When is the two-pointer technique (opposite-end pointers converging inward) most applicable?

Ready to Build This?

Level 9: Two Pointers & Sliding Window asks you to solve classic two-pointer and sliding-window array problems - exactly the two_sum_sorted, max_sum_subarray, and variable-window patterns walked through across this module.

ScaleDojo Logo
Initializing ScaleDojo