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:
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
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.

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
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
n is the total number of items in the set.
k 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:
(n−k)!n!: This is the formula for Permutations. It calculates all possible ordered arrangements.
k!1: This is the "Deduplication" factor. Since the order doesn't matter in combinations, you divide by 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 n identical items into k distinct bins.
Stars (⋆): The items you are distributing.
Bars (∣): The dividers that create the bins. To make k bins, you need k−1 bars.
2. The Two Standard Scenarios
Scenario A: Bins can be Empty (Non-negative)
Problem: x1+x2+⋯+xk=n, where xi≥0.
Logic: You have n stars and k−1 bars. You are choosing positions for the bars out of a total of (n+k−1) spots.
Formula:
Scenario B: Bins MUST have at least 1 (Positive) (No Empty bins)
Problem: x1+x2+⋯+xk=n, where xi≥1.
Logic: You place the bars in the "gaps" between stars. There are n−1 gaps, and you need to pick k−1 of them.
Formula:
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
nthen the legal positions in which the bars can be placed aren-1jdIllegal 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 (k−1n−1)
Twist 1: The "Custom Minimums" (Reduction)
Scenario: Each bin i must have at least ci items. (e.g., x1≥2,x2≥5).
The Logic:
Think of this as a pre-allocation. Before you start "choosing" positions, you must "pay" the minimums.
You take the required items out of the bag and put them in the bins.
The items left in your bag (n−∑ci) are now "free stars."
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:
Twist 2: The "Inequality" (The Dummy Bin)
Scenario: x1+x2+x3≤10. (The items don't have to be used up).
The Logic:
In standard Stars and Bars, you must use all n stars. To handle an "up to n" problem, we create a "Waste Bin" (or Dummy Bin).
Any star you don't want to give to x1,x2, or x3, you throw into the Waste Bin (x4).
The equation becomes: x1+x2+x3+xwaste=10.
The Pool: You haven't changed the number of stars (n), but you have added one extra bin (k+1)
Formula:
Twist
The Logic
The Shift
Minimums
Pre-allocate items
n decreases
Inequality
Add a "Waste Bin"
k 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)
How to calculate each part:
Using the standard Stars and Bars formula C(n,k)=(k−1n+k−1):
Total: Use all n items Total=(k−1n+k−1)
IllegalA(Bin A breaks its limit):
Give Bin A (MA+1) items first.
IllegalA=(k−1(n−(MA+1))+k−1)
IllegalB (Bin B breaks its limit):
Give Bin B (MB+1) items first.
IllegalB=(k−1(n−(MB+1))+k−1)
IllegalA and B (Both break limits):
Give Bin A (MA+1) AND give Bin B (MB+1) items first.
IllegalA and B=(k−1(n−(MA+1)−(MB+1))+k−1)
Feature
Formula
Logic
Bins can be empty
(k−1n+k−1)
Stars and Bars are mixed.
Bins must have ≥1
(k−1n−1)
Bars only go in the n−1 gaps.
Custom Minimums
(k−1nrem+k−1)
Pre-allocate, then distribute.
Inequality (≤)
(kn+k)
Add a "Waste Bin" (k becomes k+1).
Upper Bound (Max)
Total−Illegal
Force the violation, then subtract.
🛠 The Logic Cheat Sheet
1. The Core Formulas
Constraint
Logic
Formula
Non-negative (xi≥0)
Stars and bars are all movable
(k−1n+k−1)
Positive (xi≥1)
Bars only in the gaps
(k−1n−1)
Inequality (∑xi≤n)
Add a "Waste Bin"
(kn+k)
2. The "Pre-Allocation" Rule (Minima)
If each bin i needs ci items:
Reduce n: nnew=ntotal−∑ci
Apply Formula: (k−1nnew+k−1)
3. The "Inclusion-Exclusion" Rule (Maxima)
If bin i has a maximum capacity Mi:
Total: Calculate ways ignoring the max.
Violate: For a specific bin to be "illegal," give it Mi+1 items first.
Combine: Total−(Single Violations)+(Double Violations)−…
The Correct Formula for 3 Bins
Expanded out, it looks like this:
(+) Total: All possible ways.
(-) Singles: Subtract cases where A is bad, B is bad, or C is bad.
−(IllegalA+IllegalB+IllegalC)
(+) 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)
(-) Triples: Subtract the case where all three are bad.
−(IllegalA&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 k bins require k−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 kn instead.
Edge Cases: Always mention n<0 (impossible distribution) and n<k (in a Scenario B "no empty bins" problem).
Last updated