8.1 Counting Principles, Permutations, Combinations & Stars and Bars
π‘ Core Intuition: The 3-Second Mental Modelβ
π³ The Everyday Analogy: Fast Food Menus, Passwords & Coin Jarsβ
Think of counting as structuring choices in physical spaces:
- Rule of Sum (Mutually Exclusive Alternatives): You are buying lunch. You can pick either a burger from the 5 burger options OR a salad from the 3 salad options. Total independent meal choices: . In choice, you use addition (+).
- Rule of Product (Sequential Multi-Stage Choices): You are building a combo meal. You pick 1 of 5 burgers, AND 1 of 4 drinks, AND 1 of 3 sides. Total compound configurations: unique meals! In mandatory sequential stages, you use multiplication ().
- Permutation vs Combination:
- Permutation (Order Matters): Setting a 4-digit security PIN (
1-2-3-4is completely different from4-3-2-1). Permutation = Choose and Arrange. - Combination (Order Irrelevant): Picking a 4-person study team (Alice, Bob, Charlie, Dave is the exact same team regardless of who you named first). Combination = Choose Only.
- Permutation (Order Matters): Setting a 4-digit security PIN (
- Stars and Bars (Distributing Identical Candies): You have 10 identical gold coins () to drop into 3 piggy banks (). You don't label the coins; you simply place 2 divider sticks (bars) between the 10 coins (stars). Any arrangement of stars and bars uniquely represents an integer partition!
π» Bridging to Computer Science: Address Spaces, Combinatorial Testing & State Spacesβ
In computer systems:
- IPv4 / IPv6 Address Spaces: Calculating address capacities uses the Rule of Product: an IPv4 address has unique host addresses.
- Complexity Analysis: Measuring the search space of NP-complete problems (e.g., Travelling Salesperson has tours; Knapsack has subsets).
- Resource Allocation in OS: Distributing identical buffer pages among active processes is the classical Stars and Bars problem.
- Compiler Symbol Tables: Lexicographic indexing and keyword permutations determine dictionary search efficiency.
πTable of Contents
- β‘ Fundamental Counting Principles
- π Permutations with Repeated Elements (Multi-Sets)
- π§© Advanced Arrangement Paradigms: The String & Gap Methods
- π Circular Permutations & Symmetries
- π Lexicographic Word Rank & The Fast Factorial Trick
- π’ Digit Permutations & The Master Sum Formula
- β The Stars and Bars Theorem (Integer Partitions)
- π Geometric Combinatorics & Shortcuts
- π Step-by-Step Worked Examples
- π― Exam & Interview Pitfall Check
β‘ Fundamental Counting Principlesβ
- The Rule of Sum (Disjoint Unions - OR Rule): If a task can be performed in either of two mutually exclusive ways, where the first way has possible outcomes and the second way has possible outcomes (with no overlap):
- The Rule of Product (Cartesian Multi-Stage Execution - AND Rule): If a procedure can be broken down into two successive stages, where Stage 1 has possible choices, and for each choice in Stage 1, Stage 2 has possible choices:
The 4 Fundamental Combinatorial Counting Archetypes
Matrix comparing ordered vs unordered selection with and without replacement
Permutations (No Repetition)
Ordered, Distinct- Select and arrange r distinct items from n available items.
- Formula: P(n, r) = n! / (n - r)!.
- Example: Assigning 1st, 2nd, and 3rd place podium medals.
Permutations (With Repetition)
Ordered, Multi-Set- Each of the r positions independently picks any of the n items.
- Formula: nΚ³.
- Example: Generating an r-digit numeric PIN using digits 0-9 (10Κ³).
Combinations (No Repetition)
Unordered, Distinct- Select r items from n distinct items where sequence does not matter.
- Formula: C(n, r) = n! / (r! (n - r)!).
- Example: Selecting a 5-card poker hand from a 52-card deck.
Combinations (With Repetition)
Stars and Bars- Select r items from n types where items of same type are indistinguishable.
- Formula: C(n + r - 1, r) = C(n + r - 1, n - 1).
- Example: Distributing identical jobs across CPU execution queues.
π Permutations with Repeated Elements (Multi-Sets)β
When arranging total objects where some objects are identical:
The number of unique permutations of objects where there are identical objects of type 1, identical objects of type 2, ..., and identical objects of type (such that ) is given by the Multinomial Coefficient:
Proof Intuition: If all objects were distinct, there would be arrangements. However, permuting the identical objects among themselves in ways produces visually indistinguishable arrangements. By the Division Rule, we divide out all internal symmetries .
π§© Advanced Arrangement Paradigms: The String & Gap Methodsβ
Two standard arrangement constraints appear universally in competitive exams and interview questions:
The String Method vs The Gap Method
Handling 'always together' vs 'never together' constraints in linear seating
The String / Tie Method
Items MUST Be Together- Bundle all items that must stay together into a single composite super-unit.
- Step 1: Arrange the remaining items along with the composite super-unit.
- Step 2: Multiply by the internal permutations of the items inside the super-unit.
- Formula: (Remaining Items + 1)! Γ (Bundle Size)!
The Gap Method
NO Two Items Together- Step 1: First arrange the unconstrained items in a row in m! ways.
- Step 2: This creates m + 1 distinct empty gaps (including both ends).
- Step 3: Select and arrange the k constrained items into these m + 1 gaps.
- Formula: m! Γ P(m + 1, k) (Valid if and only if k β€ m + 1).
Let there be 5 boys () and 4 girls ():
- No two girls sit together (Gap Method):
- First arrange the 5 boys: ways.
- The boys create gaps: .
- Place the 4 girls into any 4 of these 6 gaps: ways.
- Total ways = ways.
- No two boys sit together (Gap Method):
- First arrange the 4 girls: ways.
- The girls create gaps: .
- Place the 5 boys into these 5 gaps: ways.
- Total ways = ways.
- A particular pair of girls () must always sit together (String Method):
- Bundle into a single composite unit: .
- Entities to arrange: 5 boys + 2 remaining girls + 1 composite unit = 8 entities.
- External arrangements: ways.
- Internal permutations inside the bundle: ways ( or ).
- Total ways = ways.
- A particular pair of girls () must NOT sit together (Complementary Counting):
- Total unrestricted arrangements of 9 people: .
- Arrangements where sit together: .
- Total ways = ways.
π Circular Permutations & Symmetriesβ
Arranging items around a closed circle differs fundamentally from linear arrangements because circular rotations do not produce distinct arrangements.
- Circular Permutation of Distinct Entities: Fixing 1 reference entity removes the rotational degrees of freedom:
- Necklaces & Keyrings (Reflection / Flip Invariance): If a necklace can be flipped over, clockwise and counter-clockwise arrangements become identical mirror images: With beads of color 1, of color 2, ..., of color :
- The Circular Gap Method (Crucial Rule):
- When the first group of items is arranged in a circle, it takes ways.
- Crucial Invariant: Once the first group is seated in the circle, the gaps between them become linearly distinct (each gap is indexed by the specific unique neighbors to its left and right).
- Therefore, the second group of items is placed into the gaps in linear ways!
- Total ways = .
Let 7 girls and 6 boys be seated around a circular table:
- No two boys sit together:
- First seat the 7 girls around the circular table: ways.
- The 7 girls create 7 distinct gaps between them.
- Seat the 6 boys into these 7 distinct gaps: ways.
- Total ways = ways.
- Boys and girls sit alternately ( boys and girls):
- Seat 7 boys in circle: ways.
- Seat 7 girls in the 7 gaps: ways.
- Total ways = ways.
π Lexicographic Word Rank & The Fast Factorial Trickβ
The rank of a word is its 1-based index when all unique anagrams formed by its letters are sorted alphabetically in a dictionary.
Dictionary Rank Algorithms
Classical tree counting versus the rapid right-hand factorial formula
Standard Lexicographic Counting
Branch-by-Branch- Sort all characters in alphabetical order.
- Count all permutations starting with characters preceding the target's first letter.
- Fix the matched letter and recursively count sub-branches.
- Add +1 at the end for the word itself.
Fast Factorial Shortcut Trick
O(n) Direct Evaluation- Write the word. For each letter, count how many letters strictly to its RIGHT are alphabetically smaller.
- Multiply each count by the corresponding descending factorial: (n-1)!, (n-2)!, ..., 0!.
- For repeated letters, divide each term by the repeat factorials in the suffix.
- Sum all products and add +1.
Target word: SIPRON (Length ). Alphabetical order of letters: .
Applying the Fast Factorial Trick:
- Letter S: Letters to its right smaller than S: (Count = 5). Multiplier: . .
- Letter I: Letters to its right smaller than I: none (Count = 0). Multiplier: . .
- Letter P: Letters to its right smaller than P: (Count = 2). Multiplier: . .
- Letter R: Letters to its right smaller than R: (Count = 2). Multiplier: . .
- Letter O: Letters to its right smaller than O: (Count = 1). Multiplier: . .
- Letter N: Letters to its right smaller than N: none (Count = 0). Multiplier: . .
Target word: SECRET (Length ). Letters: . Repeats: appears 2 times.
- S: Smaller to right: (Count = 3). Suffix has with two 's: . (Wait: check letters to right: E, C, R, E: smaller than S are C, E, R, E letters smaller!) Let's check strictly: are all smaller than . That is 4 letters! Term = .
- E: Suffix is . Smaller to right: (Count = 1). Suffix has no repeats ( has one remaining): . (Note: if both E's were in suffix, divide by ; here only one E remains, so divide by ). Term = if counting both: since the current letter is , the suffix has one . So repeats = . (if second counts).
- C: Suffix . Smaller to right: none (0). Term = 0.
- R: Suffix . Smaller to right: (Count = 1). Term = .
- E: Suffix . Smaller to right: none (0). Term = 0.
- T: Suffix empty. Term = 0.
Sum of terms = .
π’ Digit Permutations & The Master Sum Formulaβ
Given distinct non-zero digits , the sum of all -digit numbers formed using each digit exactly once without repetition is:
Proof Derivation:
- In an -digit number, each digit appears at each decimal position (units, tens, hundreds, ..., ) exactly times.
- The total contribution of all digits at a single decimal place is .
- Summing across all place values:
Digits: ().
- .
- Sum of digits: .
- Place multiplier: .
β The Stars and Bars Theorem (Integer Partitions)β
The Stars and Bars method (also known as the Bose-Einstein distribution) counts the number of ways to distribute identical items into distinct containers.
-
Non-Negative Integer Solutions (): The number of solutions to where is:
Visual Derivation: stars () and divider bars () give total positions. Choosing positions for the bars partitions the stars into distinct buckets.
-
Strictly Positive Integer Solutions (): The number of solutions to where is:
Derivation: Pre-allocate 1 item to each of the containers, leaving items to distribute freely with : .
-
General Lower Bounds (): Substitute . The equation becomes:
Problem: Find the number of ways 3 standard 6-sided dice can yield a sum of 10. Equation: subject to for each die.
- Step 1: Total positive integer solutions ():
- Step 2: Subtract violations where some die exceeds 6 ():
- Suppose Die 1 violates the constraint: .
- Set . The equation becomes:
- Number of solutions: .
- By symmetry, any of the 3 dice could violate: .
- Note: Can two dice simultaneously violate? If two dice are , sum , which is impossible.
- Step 3: Apply Principle of Inclusion-Exclusion:
π Geometric Combinatorics & Shortcutsβ
- Diagonals of an -sided Polygon: Connecting any 2 vertices gives a line segment (). Subtracting the perimeter edges:
- Intersections of Straight Lines: Given straight lines in a plane with no two parallel and no three concurrent:
- Parallelograms in a Grid: Given a family of parallel lines intersecting another family of parallel lines:
- Collinear Points in a Plane:
Given coplanar points where points are strictly collinear (and no other 3 are collinear):
- Straight Lines Formed: (the points collapse to 1 single line).
- Triangles Formed: (collinear points cannot form triangles).
- Round-Robin Tournament Matches: In a tournament of teams where each team plays every other team exactly times:
- Selection from Multi-Sets (Alphabet Selection): Given a multi-set of identical items , the number of ways to select one or more items is: (Subtract 1 for the empty selection where 0 items of each type are chosen).
π Step-by-Step Worked Examplesβ
Example 1: Multi-Set Permutations of "MISSISSIPPI" Under Adjacency Restrictionsβ
Problem Statement: Find the number of unique arrangements of all letters in the word "MISSISSIPPI":
- With no restrictions.
- Such that all four 'S' letters appear together.
- Such that no two 'S' letters appear together.
Step-by-Step Solution: Letter counts: (Total ).
- Part 1: Unrestricted Arrangements:
- Part 2: All Four 'S' Together (String Method):
- Bundle four S's into (1 unit).
- Remaining letters: letters.
- Total units = units.
- Part 3: No Two 'S' Together (Gap Method):
- First arrange the non-S letters: (7 letters).
- These 7 letters create gaps:
- Choose 4 of these 8 gaps for the 4 identical S letters: .
- Total arrangements = .
Example 2: Distributing Identical OS Memory Frames via Stars and Barsβ
Problem Statement: An operating system has 15 identical memory frames to distribute among 4 processes ().
- In how many ways can the frames be distributed if a process is allowed to receive 0 frames?
- In how many ways can the frames be distributed if process must receive at least 2 frames, and every other process must receive at least 1 frame?
Step-by-Step Solution:
- Part 1: Non-Negative Integer Solutions ():
- Equation: with .
- Formula: ways.
- Part 2: Lower-Bound Inequality ():
- Pre-allocate the required frames: frames.
- Remaining frames to distribute freely: frames.
- Transformed equation: with .
- Formula: ways.
π― Exam & Interview Pitfall Checkβ
| Combinatorial Problem Scenario | Correct Mathematical Formula | Common Incorrect Trap |
|---|---|---|
| Distribute identical balls into distinct bins | (Stars and Bars) | Confusing with (which is for distinct balls) |
| Distribute distinct balls into distinct bins | (Rule of Product) | Using |
| Seating people in a circle | Using (forgetting rotational symmetry) | |
| Circular gap method with items in circle | Treating circular gaps as gaps instead of linear gaps | |
| Diagonals of -sided polygon | Using (forgetting to subtract edges) | |
| with | Using without subtracting pre-allocations |
Question 1: What is the mathematical difference between distributing identical balls into distinct boxes versus distinct balls into distinct boxes? Key Focus Points:
- Identical balls into distinct boxes: Stars and Bars! Formula: . Only the count of balls landing in each box matters.
- Distinct balls into distinct boxes: Rule of Product! Each unique ball independently chooses any of the boxes: .
Question 2: How many terms are in the expansion of ? Key Focus Points: Each term in the multinomial expansion has the form where with . The number of distinct terms is exactly the number of non-negative integer solutions to this equation:
Trap 1: Treating Gaps in Circular Permutations as Circular Gaps Why it fails: When people sit in a circle, there are circular arrangements. But once they sit, each gap is uniquely bounded by two specific people (e.g., between Alice and Bob vs between Bob and Charlie). The gaps are therefore linearly distinct! The remaining people are arranged in ways, NOT .
Trap 2: Forgetting to Subtract Non-Existent Triangles from Collinear Points Why it fails: If points out of are collinear, students often write . Any 3 points chosen from the collinear points form a straight line, not a triangle! You must subtract .