0Pricing
Coding Interview Prep · Lesson

Sum Any Range with Subtraction

Answer range[l..r] in constant time.

Sum Any Range with Subtraction is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Real Payoff

Building the prefix array was the setup. Now comes the magic: answering any range sum with a single subtraction. ⚡

The Core Idea

A range sum is just one big total minus a smaller one. Subtracting two prefix values cancels everything outside your range cleanly.

The Formula

To sum elements from l to r, take prefix[r + 1] minus prefix[l]. This single formula works for every range.

range_sum = prefix[r + 1] - prefix[l]

Why It Works

prefix[r + 1] holds everything up to r, and prefix[l] holds everything before l. The difference leaves exactly the middle slice.

A Worked Example

For [3, 1, 4] the prefix is [0, 3, 4, 8]. To sum indices 1 to 2, do 8 minus 3, which gives 5. That matches 1 plus 4.

Constant Time Queries

Each query is just one subtraction, so it runs in O(1). A thousand queries cost the same per query as one.

Mind the Off-by-One

The most common slip is the index on the high end. With a leading zero you always use prefix[r + 1], not prefix[r]. Guard that boundary.

Inclusive vs Exclusive

Decide early whether r is included. This formula treats the range as inclusive of both l and r, which most contest problems expect.

Wrap It in a Function

A tiny helper keeps your logic readable and your indices in one place. Lean on this helper instead of inlining the math.

def query(l, r):
    return prefix[r + 1] - prefix[l]

Handle the Whole Array

To sum the entire array, query l equals 0 and r equals n minus 1. The formula gives prefix[n], the grand total.

Where This Shines

Any time a problem fires many range sum questions at a fixed array, prefix sums turn an O(n) loop per query into instant answers.

Quick Check

You want the sum of indices l through r inclusive.

Recap

You can now answer any range sum in O(1) with prefix[r + 1] minus prefix[l]. Mind the leading-zero offset and you are bug-free. ✅

Frequently asked questions

Is the “Sum Any Range with Subtraction” lesson free?

Yes — the full text of “Sum Any Range with Subtraction” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Sum Any Range with Subtraction”?

Answer range[l..r] in constant time. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sum Any Range with Subtraction” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Build a Prefix Sum Array
  2. Sum Any Range with Subtraction
  3. Count Subarrays with a Target Sum
  4. Difference Arrays for Range Updates
← Back to Coding Interview Prep