Combinatorics

Basic Counting

If there are m outcomes for event A and n outcomes for event B, then the total number of outcomes for both event A and B are:

totalNumberOfOutcomes=mntotalNumberOfOutcomes = m * n

And if there are more events like o outcomes for event C then we simply need to multiply to get the total number of outcomes

totalNumberOfOutcomes=mn0totalNumberOfOutcomes = m * n * 0

Permutations

A way in which a set of things can be ordered

When there are duplicate elements and we are tasked to find the different permutations, then the total number of elements factorial is taken and is divided by all the possible factorials of each unique element.


circle-info

The major difference between permutation and combination is 1. Permutation is arrangement (ORDER matters) 2. Combination is selection (ORDER DOES NOT MATTER)


Combinations

The number of groups of r objects that can be formed from total of n objects

circle-info

Ordering does not matter. Even if the group has different order but the same objects, it is still considered to be same

Its formula is represented by

C(n,k)=(nk)=n!k!(nk)!C(n, k) = \binom{n}{k} = \frac{n!}{k!(n-k)!}
  • nn is the total number of items in the set.

  • kk is the number of items to be selected.

When you are explaining this in a Google interview, break the formula down into its two logical parts:

  1. n!(nk)!\frac{n!}{(n-k)!}: This is the formula for Permutations. It calculates all possible ordered arrangements.

  2. 1k!\frac{1}{k!}: This is the "Deduplication" factor. Since the order doesn't matter in combinations, you divide by k!k! to remove all the different orderings of the same group of items.


Partitions

Dividing a set of elements into subsets

Dividing elements into groups

There are two different types of partitions

Dividing Distinct elements into groups

This type of problem involves determining how many ways you can group a set of distinct elements into distinct groups of specified sizes.

Example (Fish Problem): Imagine there are eight different fish, and three people (Jiggle Bob, Max, and Reagan) want to eat a specific number of fish: Jiggle Bob wants four fish, Max wants two, and Reagan wants two (19:47-19:58). The question is how many ways these eight different fish can be grouped into distinct groups of four, two, and two (19:58-20:06).

Stars and Bars method

This technique can be used in distributed problems.

1. The Core Concept

Use this when distributing nn identical items into kk distinct bins.

  • Stars (\star): The items you are distributing.

  • Bars (|): The dividers that create the bins. To make kk bins, you need k1k-1 bars.

2. The Two Standard Scenarios

Scenario A: Bins can be Empty (Non-negative)

  • Problem: x1+x2++xk=nx_1 + x_2 + \dots + x_k = n, where xi0x_i \ge 0.

  • Logic: You have nn stars and k1k-1 bars. You are choosing positions for the bars out of a total of (n+k1)(n + k - 1) spots.

  • Formula:

(n+k1k1)\binom{n + k - 1}{k - 1}

Scenario B: Bins MUST have at least 1 (Positive) (No Empty bins)

  • Problem: x1+x2++xk=nx_1 + x_2 + \dots + x_k = n, where xi1x_i \ge 1.

  • Logic: You place the bars in the "gaps" between stars. There are n1n-1 gaps, and you need to pick k1k-1 of them.

  • Formula:

(n1k1)\binom{n - 1}{k - 1}
Gap 1Gap 2Gap 3\star \underbrace{\downarrow}_{\text{Gap 1}} \star \underbrace{\downarrow}_{\text{Gap 2}} \star \underbrace{\downarrow}_{\text{Gap 3}} \star
  • In this case, there is a constraint that each bin but have atleast one element. So we need to elimenate the illegal positions and only consider the legal positions.

  • The legal positions for the bars are the gaps as shown above. If number of stars are n then the legal positions in which the bars can be placed are n-1 jd

    • Illegal Positions of bars:

      • At the very ends

      • Adjacent to each other

  • So now, we need to select the positions in which we can place the bars which is a function of (n1k1)\binom{n-1}{k-1}

Twist 1: The "Custom Minimums" (Reduction)

Scenario: Each bin ii must have at least cic_i items. (e.g., x12,x25x_1 \ge 2, x_2 \ge 5).

The Logic:

Think of this as a pre-allocation. Before you start "choosing" positions, you must "pay" the minimums.

  1. You take the required items out of the bag and put them in the bins.

  2. The items left in your bag (ncin - \sum c_i) are now "free stars."

  3. Because the "contract" is already satisfied for every bin, these remaining stars can be distributed however you like—including giving zero to a bin.

The Pool: You are now in Scenario A with a smaller number of stars.

Formula:

((npre-allocated)+k1k1)\binom{(n - \text{pre-allocated}) + k - 1}{k - 1}

Twist 2: The "Inequality" (The Dummy Bin)

Scenario: x1+x2+x310x_1 + x_2 + x_3 \le 10. (The items don't have to be used up).

The Logic:

In standard Stars and Bars, you must use all nn stars. To handle an "up to nn" problem, we create a "Waste Bin" (or Dummy Bin).

  • Any star you don't want to give to x1,x2, or x3x_1, x_2, \text{ or } x_3, you throw into the Waste Bin (x4x_4).

  • The equation becomes: x1+x2+x3+xwaste=10x_1 + x_2 + x_3 + x_{waste} = 10.

The Pool: You haven't changed the number of stars (n)(n), but you have added one extra bin (k+1)(k+1)

Formula:

(n+(k+1)1(k+1)1)(n+kk)\binom{n + (k+1) - 1}{(k+1) - 1} \rightarrow \binom{n+k}{k}

Twist

The Logic

The Shift

Minimums

Pre-allocate items

nn decreases

Inequality

Add a "Waste Bin"

kk increases

Maximums

Subtract illegal states

Use Inclusion-Exclusion

Inclusion Exclusion Principle

This technique is used when there is a constraint like a particular bin/s have upper bound(max)

Valid Ways=Total(IllegalA+IllegalB)+IllegalA and B\text{Valid Ways} = \text{Total} - (\text{Illegal}_A + \text{Illegal}_B) + \text{Illegal}_{A \text{ and } B}

How to calculate each part:

Using the standard Stars and Bars formula C(n,k)=(n+k1k1)C(n, k) = \binom{n+k-1}{k-1}:

  1. Total: Use all nn items Total=(n+k1k1)\text{Total} = \binom{n+k-1}{k-1}

  2. IllegalA\text{Illegal}_A (Bin A breaks its limit):

    Give Bin A (MA+1)(M_A + 1) items first.

    IllegalA=((n(MA+1))+k1k1)\text{Illegal}_A = \binom{(n - (M_A + 1)) + k - 1}{k - 1}

  3. IllegalB\text{Illegal}_B (Bin B breaks its limit):

    Give Bin B (MB+1)(M_B + 1) items first.

    IllegalB=((n(MB+1))+k1k1)\text{Illegal}_B = \binom{(n - (M_B + 1)) + k - 1}{k - 1}

  4. IllegalA and B\text{Illegal}_{A \text{ and } B} (Both break limits):

    Give Bin A (MA+1)(M_A + 1) AND give Bin B (MB+1)(M_B + 1) items first.

    IllegalA and B=((n(MA+1)(MB+1))+k1k1)\text{Illegal}_{A \text{ and } B} = \binom{(n - (M_A + 1) - (M_B + 1)) + k - 1}{k - 1}

Feature

Formula

Logic

Bins can be empty

(n+k1k1)\binom{n+k-1}{k-1}

Stars and Bars are mixed.

Bins must have 1\ge 1

(n1k1)\binom{n-1}{k-1}

Bars only go in the n1n-1 gaps.

Custom Minimums

(nrem+k1k1)\binom{n_{rem} + k - 1}{k - 1}

Pre-allocate, then distribute.

Inequality (\le)

(n+kk)\binom{n+k}{k}

Add a "Waste Bin" (kk becomes k+1k+1).

Upper Bound (Max)

TotalIllegalTotal - Illegal

Force the violation, then subtract.


🛠 The Logic Cheat Sheet

1. The Core Formulas

Constraint

Logic

Formula

Non-negative (xi0x_i \ge 0)

Stars and bars are all movable

(n+k1k1)\binom{n+k-1}{k-1}

Positive (xi1x_i \ge 1)

Bars only in the gaps

(n1k1)\binom{n-1}{k-1}

Inequality (xin\sum x_i \le n)

Add a "Waste Bin"

(n+kk)\binom{n+k}{k}

2. The "Pre-Allocation" Rule (Minima)

If each bin ii needs cic_i items:

  1. Reduce nn: nnew=ntotalcin_{new} = n_{total} - \sum c_i

  2. Apply Formula: (nnew+k1k1)\binom{n_{new} + k - 1}{k - 1}

3. The "Inclusion-Exclusion" Rule (Maxima)

If bin ii has a maximum capacity MiM_i:

  1. Total: Calculate ways ignoring the max.

  2. Violate: For a specific bin to be "illegal," give it Mi+1M_i + 1 items first.

  3. Combine: Total(Single Violations)+(Double Violations)\text{Total} - (\text{Single Violations}) + (\text{Double Violations}) - \dots

The Correct Formula for 3 Bins

Valid Ways=Total(Singles)+(Pairs)(Triples)\text{Valid Ways} = \text{Total} - (\text{Singles}) + (\text{Pairs}) - (\text{Triples})

Expanded out, it looks like this:

  1. (+) Total: All possible ways.

  2. (-) Singles: Subtract cases where A is bad, B is bad, or C is bad.

    • (IllegalA+IllegalB+IllegalC)- (\text{Illegal}_A + \text{Illegal}_B + \text{Illegal}_C)

  3. (+) Pairs: Add back cases where two are bad at the same time (because you subtracted these twice in the step above).

    • +(IllegalA&B+IllegalB&C+IllegalA&C)+ (\text{Illegal}_{A\&B} + \text{Illegal}_{B\&C} + \text{Illegal}_{A\&C})

  4. (-) Triples: Subtract the case where all three are bad.

    • (IllegalA&B&C)- (\text{Illegal}_{A\&B\&C})


💻 The Implementation Cheat Sheet

Option A: Pascal's Identity (Best for multiple lookups)

Prevents overflow by using addition instead of multiplication.

Option B: The Bitmask PIE (Best for Multiple Max Limits)


🧠 Final Interview Tips

  • The "Bar" Rule: Always remember that kk bins require k1k-1 bars. The "bottom" number of your combination is almost always the number of bars.

  • The "Item" Identity: Ensure the items are identical (e.g., tasks, requests, coins). If the items are distinct (e.g., people, unique IDs), Stars and Bars does not apply; use knk^n instead.

  • Edge Cases: Always mention n<0n < 0 (impossible distribution) and n<kn < k (in a Scenario B "no empty bins" problem).

Last updated