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.
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
What you’ll learn · Before you start
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
On this page
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.
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
- 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.
- At least one. A thali comes with 5 side dishes, and a diner takes at least one: 2⁵ − 1 = 31 choices.
- 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.
- 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.
- 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 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
Runs on this device, in your browser. The first run downloads Python (about 13.5 MB), which is kept for the next runs.
Your run, in this browser
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 putnidentical items intoboxesdifferent boxes so that every box holds at leastlowand, whenhighis given, at mosthighitems.distributions(10, 3)is 66 anddistributions(10, 3, low=1)is 36, the lesson's sweets.derangements(n)returns how many orders ofnletters put no letter in its own envelope:derangements(4)is 9. With no letters at all there is exactly one way, soderangements(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)).
Results of the sample tests
| Test | Result | Details |
|---|
What your code printed
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.
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.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
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.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
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.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
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
-
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.
-
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?
-
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.
-
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.
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
- Mathematics, Class 11, Chapter 6: Permutations and Combinations (National Council of Educational Research and Training (NCERT))
- NIST Digital Library of Mathematical Functions, §26.3: Binomial Coefficients (National Institute of Standards and Technology (NIST))
- NIST Digital Library of Mathematical Functions, §26.8: Stirling Numbers (National Institute of Standards and Technology (NIST))
- NIST Digital Library of Mathematical Functions, §26.13: Permutations and derangements (National Institute of Standards and Technology (NIST))
- NIST Digital Library of Mathematical Functions, §26.17: The Twelvefold Way (National Institute of Standards and Technology (NIST))
- itertools.combinations and itertools.product (Python Software Foundation)
- math.comb (Python Software Foundation)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress