Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Placement Aptitude (Quant, Logical, Verbal)  Module 9 – Counting, probability and statistics

Combinations, selections and distributions

Choose with nCr, count at least and at most by cases or the complement, share identical items by stars and bars, and count derangements.

  • Advanced
  • 35 minutes
  • Examples run with Node.js 24.21.0, Python 3.14.8 and Pyodide 314.0.7
  • By MySmartCoPilot
Free core, Pro depth: 12 of 35 minutes free
Free: Choosing: nCr, at least and at most · Distributions: identical or different items and boxes · Derangements, and the check against counting twice · 2 more sections. With a pass: 3 in-depth parts and 3 interview answers, about 23 minutes.

What you will learn

  • Count selections with nCr, including 'at least' and 'at most' cases
  • Count the ways to distribute identical and distinct items into boxes
  • Count derangements and avoid overcounting

Before you start

Combinations count selections in which order does not matter: a team, a committee, a plate of side dishes. Choosing r of n different objects can be done in nCr = n!/(r!(n − r)!) ways, so a 3-member team from 8 students can be picked in 56 ways, while filling 3 different posts from the same 8 gives 336, because each team can be ordered in 3! = 6 ways. Selections with a condition, such as “at least two women”, are counted by cases or as the total minus what is not allowed. Distributions put items into boxes, and the count depends on whether the items, and the boxes, are identical or different. Derangements count the orders in which nothing is in its own place.

Choosing: nCr, at least and at most

Combinations and arrangements are linked by nPr = nCr × r!, since each selection of r objects can be put in r! orders. Two facts save time. nCr = nC(n − r): choosing the 3 who stay back from 10 is the same count as choosing the 7 who go. And the selections of every size add up to 2ⁿ, because each object is either in or out. “At least one” is almost always easiest as the total minus “none”. For “at least” and “at most” with two groups, add the allowed cases or subtract the forbidden ones, and use one method to check the other.

Distributions: identical or different items and boxes

Distributions of identical items into different boxes use stars and bars: the n items and r − 1 bars stand in a row, and choosing the bars’ places gives C(n + r − 1, r − 1) ways, or C(n − 1, r − 1) when no box may be empty.

Ten sweets and two bars in a row split 3, 5 and 2 among three children: C(12, 2) = 66 splits, or C(9, 2) = 36 if each child gets one.1. Any split: 10 sweets and 2 bars in 12 placesA: 3B: 5C: 2Choose the 2 bar places: C(12, 2) = 662. At least one each: bars only in the 9 gapsNo two bars share a gap, so no child is left outChoose 2 of the 9 gaps: C(9, 2) = 36

Stars and bars for 10 identical sweets and 3 children

Text description of the diagram

The figure has two rows.

The first row shows twelve places in a line: ten circles for the sweets and two vertical bars. The bars cut the line into three parts, for children A, B and C: three sweets, then five, then two. Any choice of 2 of the 12 places for the bars gives a split, and every split comes from exactly one choice, so there are C(12, 2) = 66 splits. A bar at either end, or two bars side by side, gives a child no sweets.

The second row shows the ten sweets alone, with the nine gaps between neighbouring sweets marked. Two of the gaps hold bars, again splitting the sweets 3, 5 and 2. When every child must get at least one sweet, the bars may go only into these gaps, one bar per gap, so there are C(9, 2) = 36 splits.

Items Boxes Count Example
Identical Different C(n + r − 1, r − 1) 10 sweets, 3 children: 66
Identical, none left empty Different C(n − 1, r − 1) The same sweets, one each at least: 36
Different Different rⁿ, since each item picks its box 4 different prizes, 3 students: 3⁴ = 81
Different Identical Stirling numbers S(n, r) 3 people, 2 identical rooms, none empty: 3

Stirling numbers of the second kind, S(n, r), count the ways to split n different items into r identical non-empty groups.

Derangements, and the check against counting twice

A derangement is an order in which no item is in its own place, such as letters that all go into the wrong envelopes. Their number is D(n) = n!(1 − 1/1! + 1/2! − … ± 1/n!), which gives D(3) = 2, D(4) = 9 and D(5) = 44, and each value also follows from D(n) = (n − 1)(D(n − 1) + D(n − 2)). With exactly k items in place, choose those k and derange the rest: C(n, k) × D(n − k). Before trusting any method, ask whether it could produce the same selection twice; if it could, the count is too large, and splitting into cases is safer.

Five solved examples

  1. A team with a condition. A 4-member team comes from 7 final-year and 5 pre-final-year students, with at least 2 final-year students. By cases: C(7, 2) × C(5, 2) + C(7, 3) × C(5, 1) + C(7, 4) = 210 + 175 + 35 = 420. By the complement: C(12, 4) − C(5, 4) − 7 × C(5, 3) = 495 − 5 − 70 = 420.
  2. At least one. A thali comes with 5 side dishes, and a diner takes at least one: 2⁵ − 1 = 31 choices.
  3. Points and lines. Ten points, no three in a line, give C(10, 2) = 45 lines and C(10, 3) = 120 triangles. A 12-sided polygon has C(12, 2) − 12 = 54 diagonals, since 12 of the 66 joins are its sides.
  4. Sweets. Ten identical sweets for 3 children: C(12, 2) = 66 splits, or C(9, 2) = 36 if each child gets at least one, as in the figure.
  5. Envelopes. Four letters go into four addressed envelopes. None in the right envelope: D(4) = 9. Exactly one right: choose it, then derange the other three, 4 × D(3) = 8.
The five examples, counted by listing every case Python · five/check_five.py
# The five solved examples of the lesson, each counted twice: with the formula from the text and by listing every
# selection, distribution or arrangement with itertools.
from itertools import combinations, permutations, product
from fractions import Fraction
from math import comb, factorial

# 1. A team of 4 from 7 final-year (F) and 5 pre-final-year (P) students, with at least 2 final-year students.
students = [f"F{i}" for i in range(7)] + [f"P{i}" for i in range(5)]
teams = list(combinations(students, 4))
listed = sum(1 for t in teams if sum(s[0] == "F" for s in t) >= 2)
cases = comb(7, 2) * comb(5, 2) + comb(7, 3) * comb(5, 1) + comb(7, 4)
complement = comb(12, 4) - comb(5, 4) - comb(7, 1) * comb(5, 3)
print(f"1. Team: cases 210 + 175 + 35 = {cases}, complement 495 − 5 − 70 = {complement}, listed {listed} of {len(teams)}")

# 2. A thali with 5 side dishes, at least one of them taken.
taken = sum(1 for k in range(1, 6) for _ in combinations(range(5), k))
print(f"2. At least one of 5 dishes: 2⁵ − 1 = {2 ** 5 - 1}, listed {taken}")

# 3. Ten points with no three in a line: lines and triangles; the diagonals of a 12-sided polygon.
points = range(10)
diagonals = sum(1 for i, j in combinations(range(12), 2) if (j - i) % 12 not in (1, 11))
print(f"3. Lines {comb(10, 2)} (listed {len(list(combinations(points, 2)))}), "
      f"triangles {comb(10, 3)} (listed {len(list(combinations(points, 3)))}), "
      f"diagonals of a 12-gon {comb(12, 2)} − 12 = {comb(12, 2) - 12} (listed {diagonals})")

# 4. Ten identical sweets for 3 children: any split, and at least one sweet each.
splits = [(a, b, 10 - a - b) for a in range(11) for b in range(11 - a)]
print(f"4. Sweets: C(12, 2) = {comb(12, 2)}, listed {len(splits)}; at least one each: "
      f"C(9, 2) = {comb(9, 2)}, listed {sum(1 for s in splits if min(s) >= 1)}")

# 5. Four letters in four addressed envelopes: none in its own envelope, and exactly one in its own.
def right(order):
    return sum(order[i] == i for i in range(len(order)))

def derangements(n):
    """n!(1 − 1/1! + 1/2! − … ± 1/n!), the formula for D(n)."""
    return factorial(n) * sum(Fraction((-1) ** k, factorial(k)) for k in range(n + 1))

orders = list(permutations(range(4)))
print(f"5. Envelopes: none right D4 = {derangements(4)}, listed {sum(1 for o in orders if right(o) == 0)}; "
      f"exactly one right 4 × D3 = {4 * derangements(3)}, listed {sum(1 for o in orders if right(o) == 1)}")

# Distinct prizes are a different count: 4 different prizes for 3 students, any student may get several.
print(f"Distinct prizes: 3⁴ = {3 ** 4}, listed {len(list(product(range(3), repeat=4)))}")

Output

1. Team: cases 210 + 175 + 35 = 420, complement 495 − 5 − 70 = 420, listed 420 of 495
2. At least one of 5 dishes: 2⁵ − 1 = 31, listed 31
3. Lines 45 (listed 45), triangles 120 (listed 120), diagonals of a 12-gon 66 − 12 = 54 (listed 54)
4. Sweets: C(12, 2) = 66, listed 66; at least one each: C(9, 2) = 36, listed 36
5. Envelopes: none right D4 = 9, listed 9; exactly one right 4 × D3 = 8, listed 8
Distinct prizes: 3⁴ = 81, listed 81

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 check_five.py

The program does not use the formulas for its own counts: it lists all 495 teams, every split of the sweets and every order of the letters, and keeps the ones that fit. Each listed count matches the formula beside it.

Permutation & Combination Calculator It gives nCr, stars and bars and derangements as exact counts, so you can check these examples and items of your own.

Exercise: distributions with limits, and derangements

The exercise counts distributions with a lower and an upper limit per box, and derangements of up to 15 letters, and the ten questions after it are untimed.

Exercise · Medium · Python

Count distributions with limits, and derangements

Write two functions in counts.py, the file the tests import.

  • distributions(n, boxes, low=0, high=None) returns the number of ways to put n identical items into boxes different boxes so that every box holds at least low and, when high is given, at most high items. distributions(10, 3) is 66 and distributions(10, 3, low=1) is 36, the lesson's sweets.
  • derangements(n) returns how many orders of n letters put no letter in its own envelope: derangements(4) is 9. With no letters at all there is exactly one way, so derangements(0) is 1.

The tests include 60 items in 6 boxes and 15 letters, so count with a formula or a table of smaller cases instead of listing every distribution or order.

Starter code · counts.py

from functools import cache


def distributions(n, boxes, low=0, high=None):
    """Ways to put n identical items into the given number of different boxes, each holding low to high items."""
    # Replace this line with your code.
    return 0


def derangements(n):
    """Orders of n letters in which no letter is in its own envelope."""
    # Replace this line with your code.
    return 0
The sample tests · test_counts.py
from counts import derangements, distributions


def test_the_lesson_sweets():
    """10 identical sweets for 3 children, then at least one each"""
    assert distributions(10, 3) == 66
    assert distributions(10, 3, low=1) == 36


def test_an_upper_limit():
    """7 items in 3 boxes holding at most 3 each, and a range of 2 to 8 items per box"""
    assert distributions(7, 3, high=3) == 6
    assert distributions(20, 4, low=2, high=8) == 231


def test_nothing_and_impossible_cases():
    """No items, and minimums that need more items than there are"""
    assert distributions(0, 3) == 1
    assert distributions(5, 2, low=3) == 0


def test_a_large_case_without_listing():
    """60 items in 6 boxes of at most 20"""
    assert distributions(60, 6, high=20) == 2248575


def test_small_derangements():
    """No letters, one letter, then the lesson's four envelopes"""
    assert [derangements(n) for n in range(6)] == [1, 0, 1, 2, 9, 44]


def test_larger_derangements():
    """Ten and fifteen letters"""
    assert derangements(10) == 1334961
    assert derangements(15) == 481066515734
A hint

For distributions, decide how many items the first box gets, from low to high (or to everything left), and add up the ways to share the rest among the other boxes; remembering the answers for smaller cases, for example with functools.cache, keeps it fast. For derangements, start from D(0) = 1 and D(1) = 0 and use D(n) = (n − 1) × (D(n − 1) + D(n − 2)).

The sample tests run on this device, in your browser (Pyodide): nothing is sent to mysmartcopilot.com. The first run downloads Python (about 13.5 MB), which is kept for the next runs. A check in your browser is feedback for you, not proof that the code is right for every input.

Ten untimed questions on selections and distributions

10 questions about this lesson. Every answer and why it is right is on the page, behind “Show the answer”. Your score stays in this browser.

  1. Question 1 of 10 In how many ways can a group of 3 be chosen from 9 people?

    Type a number.

    Show the answer to question 1

    Answer: 84

    Order does not matter in a group, so C(9, 3) = 9 × 8 × 7 ÷ 3! = 504 ÷ 6 = 84.

  2. Question 2 of 10 Which of these equals C(20, 17)?

    Choose one answer.

    Show the answer to question 2

    Answer: C(20, 3)

    Choosing the 17 who are picked is the same as choosing the 3 who are left out, so C(20, 17) = C(20, 3) = 1,140. 20P3 = 6,840 orders the three.

  3. Question 3 of 10 A 6-member committee is chosen from 5 men and 4 women with exactly 2 women. In how many ways?

    Type a number.

    Show the answer to question 3

    Answer: 30

    Choose the 2 women from 4 and the other 4 members from the 5 men: C(4, 2) × C(5, 4) = 6 × 5 = 30.

  4. Question 4 of 10 A group of 3 is chosen from 5 men and 3 women with at least one woman. In how many ways?

    Type a number.

    Show the answer to question 4

    Answer: 46

    All groups minus the groups with no woman: C(8, 3) − C(5, 3) = 56 − 10 = 46. Picking a woman first and then any two others would give 63, because a group with two or three women is counted more than once.

  5. Question 5 of 10 Eight points lie on a circle. How many triangles have three of them as corners?

    Type a number.

    Show the answer to question 5

    Answer: 56

    No three points on a circle lie in a line, so every choice of 3 points makes a triangle: C(8, 3) = 56.

  6. Question 6 of 10 How many diagonals does an octagon have?

    Type a number.

    Show the answer to question 6

    Answer: 20

    The 8 corners have C(8, 2) = 28 joins, and 8 of them are sides, so 20 are diagonals. The formula n(n − 3)/2 gives 8 × 5 ÷ 2 = 20 as well.

  7. Question 7 of 10 In how many ways can 8 identical marbles be put into 3 different bags, if a bag may stay empty?

    Type a number.

    Show the answer to question 7

    Answer: 45

    Stars and bars: 8 marbles and 2 bars in 10 places, so C(10, 2) = 45.

  8. Question 8 of 10 In how many ways can the same 8 marbles go into the 3 bags if no bag may stay empty?

    Type a number.

    Show the answer to question 8

    Answer: 21

    The bars must go into 2 of the 7 gaps between the marbles: C(7, 2) = 21. Putting one marble in each bag first and sharing the other 5 freely gives C(7, 2) as well.

  9. Question 9 of 10 Three different gifts are given to 4 children, and a child may get more than one gift. In how many ways?

    Choose one answer.

    Show the answer to question 9

    Answer: 64

    Each gift picks its child, 4 choices three times: 4³ = 64. The answer 81 = 3⁴ lets each child pick a gift, which swaps the roles of gifts and children.

  10. Question 10 of 10 Five letters go into five addressed envelopes, one in each. In how many ways is exactly one letter in its right envelope?

    Type a number.

    Show the answer to question 10

    Answer: 45

    Choose the letter that is right, 5 ways, and derange the other four, D(4) = 9 ways: 5 × 9 = 45.

Twelve selection and distribution items with full working Pro: opens with a Pro, Premium or Ultimate pass

Selection items vary in a few set ways: a person who must be in or out, a limit on one group, groups with no names, and boxes with limits. All twelve original items below come with their full working and a quick check that confirms the count.

In this part:

  • People who must be in or out, and limits on a group, worked
  • Groups without names, and identical items
  • Equations with limits, by stars and bars
  • Different items, identical boxes and derangements
  • Points, lines and grid paths, with numbers

1 runnable example (Python) · about 3 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Overcounting reproduced: four wrong methods against a full listing Pro: opens with a Pro, Premium or Ultimate pass

Most wrong answers in selection items come from methods that count some outcomes twice, or treat different outcomes as one. This part runs four such methods against a full listing, shows exactly how far off each one is and why, and ends with the table that decides which distribution formula applies.

In this part:

  • Drill 1: “a woman first” counts each committee once per woman
  • Drill 2: choosing pair after pair counts each split 3! times
  • Drill 3: different items treated as identical
  • Drill 4: an upper limit ignored, then subtracted
  • The twelvefold way for 5 items and 3 boxes, with numbers

1 runnable example (Python) · about 2 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

A 12-minute set of ten items, with answers and a rubric Pro: opens with a Pro, Premium or Ultimate pass

Selections, distributions and derangements are mixed in these ten items, about 70 seconds apiece. Work them against a 12-minute clock, then compare your answers with the short working and read your result off the rubric.

In this part:

  • Ten mixed items, 12 minutes
  • The answers, each with its shortest working
  • What your score says, as a rubric

1 runnable example (Python) · about 2 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Interview questions

  1. Fresher · Warm-up

    How do you decide whether an item needs a permutation or a combination?

    Ask whether swapping two of the chosen objects gives a different result. If it does, as with posts, ranks, seats or the digits of a number, order matters and the count is an arrangement, nPr. If it does not, as with a team, a committee or a hand of cards, it is a selection, nCr, which is nPr divided by r!. From 8 students, a president, a secretary and a treasurer can be chosen in 8P3 = 336 ways, but a 3-member team in only C(8, 3) = 56. Mixed items select first and then arrange: a 3-member team with one of them as captain is 56 × 3 = 168.

  2. Fresher to mid

    A 5-member committee is chosen from 6 women and 7 men with at least 2 women. A classmate answers C(6, 2) × C(11, 3): pick two women, then any three others. Why is that wrong, what is the right count, and how do you check it?

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  3. Fresher

    In how many ways can 12 identical sweets go to 4 children if each child gets at least 1 and at most 5? Explain stars and bars, how you handle the upper limit, and how you would check the count.

    Model answer, Pro. First step and follow-ups

  4. Fresher to mid

    Six letters go at random into six addressed envelopes, one in each. In how many ways are exactly two letters in the right envelope, and what is the probability that no letter is? Explain derangements on the way.

    Model answer, Pro. First step and follow-ups

Key takeaways

  • nCr = n!/(r!(n − r)!) counts selections; nPr = nCr × r! orders them.
  • “At least one” is the total minus “none”; check case sums with the complement.
  • Identical items in different boxes: C(n + r − 1, r − 1), or C(n − 1, r − 1) with none empty.
  • Different items in different boxes: rⁿ.
  • Derangements: D(3) = 2, D(4) = 9, D(5) = 44; exactly k in place is C(n, k) × D(n − k).
  • If a method can count the same selection twice, switch to cases.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.