0Pricing
Coding Interview Prep · Lesson

nCr with Precomputed Factorials

Count combinations modulo a prime.

nCr with Precomputed Factorials is a free Coding Interview Prep lesson on CoddyKit — lesson 4 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.

Counting Combinations

Many problems ask how many ways to choose r items from n, written nCr. Contests want that count under a prime modulus. 🧮

The Factorial Formula

The classic formula is nCr equals n factorial divided by r factorial times n minus r factorial. The catch is that division under a mod.

# nCr = n! / (r! * (n-r)!)

Factorials Explode

A single factorial grows astronomically, so you take each one mod p. That keeps every value small while the formula stays exact under the modulus.

Precompute All Factorials

Build a fact array once up to the largest n you need. Each entry is the previous times the index, taken mod p as you go.

fact[i] = fact[i-1] * i % MOD

Division Needs Inverses

The formula divides by two factorials, so you need their modular inverses. Recall the inverse turns division into a clean multiplication.

Invert the Top Factorial

Compute the inverse of the largest factorial just once with Fermat, using pow with exponent p minus 2. That single call seeds the rest.

inv_fact[n] = pow(fact[n], MOD - 2, MOD)

Roll Inverses Backward

Get the other inverse factorials in one backward pass, each from the next times the index. No extra pow calls needed.

inv_fact[i] = inv_fact[i+1] * (i+1) % MOD

Assemble nCr

Now nCr is just fact[n] times inv_fact[r] times inv_fact[n minus r], all mod p. Three lookups and two multiplies per query.

C = fact[n] * inv_fact[r] % MOD * inv_fact[n-r] % MOD

Each Query Is Instant

After the precompute, every combination answer is O(1). That is why this pattern shines when a problem asks thousands of nCr values.

Handle the Edge Cases

If r is negative or larger than n, the answer is 0. Check that bound first so you never index outside your factorial arrays.

if r < 0 or r > n: return 0

Size Arrays Generously

Set your array size to the maximum n across all queries plus a little margin. A too-small limit is a common cause of index errors here.

N = 200005

Quick Check

After precomputing, how fast is one nCr query?

Recap

You precompute factorials and their inverses once, then answer each nCr in O(1) with three lookups. Guard the r bounds and size arrays big enough. 🏆

Frequently asked questions

Is the “nCr with Precomputed Factorials” lesson free?

Yes — the full text of “nCr with Precomputed Factorials” 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 “nCr with Precomputed Factorials”?

Count combinations modulo a prime. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “nCr with Precomputed Factorials” 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. Work Modulo a Prime
  2. Fast Modular Exponentiation
  3. Modular Inverse via Fermat
  4. nCr with Precomputed Factorials
← Back to Coding Interview Prep