Combinatorics
Probability
Binomial Coefficient
Mathematics
Permutations

Choosing k out of n

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

"Choosing k out of n" is the standard combinations problem: how many ways can you select k items from n distinct items when order does not matter. The answer is the binomial coefficient, a counting tool that shows up everywhere from probability and statistics to algorithms and dynamic programming.

Combinations Versus Permutations

The first question is always whether order matters.

If selecting A then B is considered the same outcome as selecting B then A, the problem is about combinations. If those are counted separately, the problem is about permutations.

For example:

  • choosing 3 committee members from 10 people is a combinations problem
  • choosing gold, silver, and bronze winners from 10 people is a permutations problem

The notation for combinations is:

C(n, k) or "n choose k"

The formula is:

C(n, k) = n! / (k! * (n - k)!)

That formula counts unordered selections by starting from ordered arrangements and then dividing out the internal reorderings of each chosen group.

Why the Formula Works

Suppose you choose k items from n, but temporarily treat order as important. There are:

n * (n - 1) * (n - 2) * ... * (n - k + 1)

ways to do that.

However, each group of k chosen items has been counted k! times because the same chosen set can appear in many internal orders. Dividing by k! removes that overcounting, which leads directly to the binomial coefficient formula.

This interpretation is useful because it explains the formula instead of turning it into a memorization exercise.

A Small Example

How many ways can you choose 2 items from 5?

C(5, 2) = 5! / (2! * 3!) = 10

You can list them explicitly:

  • AB
  • AC
  • AD
  • AE
  • BC
  • BD
  • BE
  • CD
  • CE
  • DE

That list makes the "order does not matter" idea concrete. There is no separate entry for BA because BA is the same choice as AB.

Symmetry Makes the Problem Easier

One of the most important identities is:

C(n, k) = C(n, n - k)

This makes intuitive sense. Choosing 2 people to include is equivalent to choosing the 8 people to leave out when n = 10.

That identity is also computationally useful. If you need C(100, 97), compute C(100, 3) instead. They are equal, and the smaller k usually makes the arithmetic simpler and safer.

Computing It in Code

In Python, the best direct option is math.comb:

python
1import math
2
3print(math.comb(5, 2))
4print(math.comb(52, 5))

If you need to implement the calculation yourself, a multiplicative loop is safer than direct factorials:

python
1def n_choose_k(n: int, k: int) -> int:
2    if k < 0 or k > n:
3        return 0
4    k = min(k, n - k)
5    result = 1
6    for i in range(1, k + 1):
7        result = result * (n - k + i) // i
8    return result
9
10print(n_choose_k(5, 2))
11print(n_choose_k(30, 6))

This avoids constructing enormous factorial values first, which is especially important in languages with fixed-width integer types.

Pascal's Rule and Dynamic Programming

Another key identity is:

C(n, k) = C(n - 1, k - 1) + C(n - 1, k)

This says every size-k selection either contains a particular chosen item or it does not. That is the logic behind Pascal's triangle.

Here is a simple dynamic-programming table:

python
1def comb_table(n: int):
2    table = [[0] * (n + 1) for _ in range(n + 1)]
3    for i in range(n + 1):
4        table[i][0] = 1
5        table[i][i] = 1
6        for j in range(1, i):
7            table[i][j] = table[i - 1][j - 1] + table[i - 1][j]
8    return table
9
10table = comb_table(6)
11print(table[6][2])
12print(table[6][3])

For one query, math.comb is simpler. For many related queries or teaching purposes, Pascal-style recursion is very helpful.

Why It Appears So Often

Combinations appear whenever you count fixed-size subsets:

  • lottery ticket counts
  • poker hands
  • feature-subset selection
  • binomial-probability calculations

For example, the number of 5-card hands from a 52-card deck is:

C(52, 5)

That is exactly the count of unordered 5-card selections from the full deck.

Common Pitfalls

The most common mistake is using permutations when the problem is actually about combinations. If order does not matter, do not count different internal orderings as separate outcomes.

Another mistake is forgetting the valid range for k. If k is negative or greater than n, the number of valid selections is zero.

People also compute large factorials directly and overflow intermediate values. Even if the final answer fits, the factorial terms may blow up first.

Finally, some learners memorize the formula without checking the problem statement. The phrase "choose k out of n" always points to unordered selection, and that conceptual check prevents many errors.

Summary

  • Choosing k out of n means counting unordered selections.
  • The formula is C(n, k) = n! / (k! * (n - k)!).
  • Symmetry gives C(n, k) = C(n, n - k), which often simplifies computation.
  • In code, built-ins such as math.comb or multiplicative loops are safer than raw factorials.
  • Always ask whether order matters before deciding which counting formula to use.

Course illustration
Course illustration

All Rights Reserved.