O(2ⁿ) means the work doubles every time one more item is added.
Video lesson · Chapter 1 of 5
Every habitat switch is independent
Walk the decision tree
Predict the jump at twenty switches
What pruning and memoization change
Distinguish time, path, and archive
0:00 / 4:28
O(2ⁿ) exponential time
1:23·Reel
Read caption
Vela visits every on/off configuration with a binary decision tree and a constant-time final check.
Another switch doubles the paths.
How it works
Step through the idea, or press Play.
Step 1 of 3
Two choices per item. Take it or leave it, or call yourself twice: every item or level splits the work in two
The tree doubles each level. After n levels there are 2ⁿ leaves, so one more item doubles the total work
Cut the repeats. Memoisation, DP, pruning or meet in the middle stop the tree from redoing the same work
Understand it
Start here
What O(2ⁿ) means
An algorithm is O(2ⁿ) when the number of operations doubles with each extra input element. Ten items cost about a thousand steps, twenty about a million, thirty about a billion. It is the usual cost of brute-force solutions that try every combination, including the brute force for many NP-hard problems.
Two choices per item
Include or exclude, left or right, call or skip: each binary decision doubles the number of paths.
+1 item, ×2 work
Going from n to n+1 doubles the cost, because 2n+1=2⋅2n.
Small n only
Fine for n≤20 or so, hopeless beyond about 40 without a smarter idea.
Where the 2ⁿ comes from
n items2×2×⋯×2=2n
Each of the n items has 2 choices, and the choices are independent, so the counts multiply. For {A,B,C} that is 23=8 subsets.
Each level decides one item (the last level decides C: left branch leaves it, right branch takes it). Three levels give 23=8 leaves, and every leaf is a different subset.
Every node in the tree
1+2+4+⋯+2n=2n+1−1=O(2n)
Level k has 2k nodes. Adding all levels gives just under twice the number of leaves: 15 nodes for 3 items. The leaves dominate, so the whole tree is still O(2n).
Scale
The exponential explosion
n
2n
Time at 1 µs per step
10
1,024
about 1 ms
20
1,048,576
about 1 second
30
1,073,741,824
about 18 minutes
40
about 1.1 trillion
about 13 days
50
about 1.1 quadrillion
about 36 years
Just 10 more items multiply the time by about a thousand.
Why +10 means ×1,000
2n+10=210⋅2n=1024⋅2n
A computer twice as fast buys you exactly one more item. A thousand times faster buys about ten.
Example 1
Generating every subset
Python
defgenerate_subsets(items): result = []defbacktrack(i, current):if i == len(items): result.append(current[:]) # a leaf: one full subsetreturnbacktrack(i + 1, current) # leave items[i] out current.append(items[i]) # take items[i]backtrack(i + 1, current) current.pop() # undo the choicebacktrack(0, [])return resultsubsets = generate_subsets(["A", "B", "C"])print(subsets) # [[], ['C'], ['B'], ['B', 'C'], ['A'], ['A', 'C'], ['A', 'B'], ['A', 'B', 'C']]print(len(subsets)) # 8 = 2 ** 3
Backtracking: leave the item out, then take it, then undo. The leaves come out in counting order: { }, {C}, {B}, {B, C}, {A}, ...Edit and run it in your browser
Each subset is a binary number
Mask (A B C)
Number
Subset
Mask (A B C)
Number
Subset
000
0
{ }
100
4
{A}
001
1
{C}
101
5
{A, C}
010
2
{B}
110
6
{A, B}
011
3
{B, C}
111
7
{A, B, C}
Counting from 0 to 2n−1 in binary visits every subset once, which is why for mask in range(1 << n) is another way to write the same search.
Total work
subsets2n×copy eachO(n)=O(n⋅2n)
There are 2n answers and each can hold up to n items, so the output alone is O(n⋅2n). The recursion stack is only O(n) deep.
Example 2
Naive Fibonacci
The cost recurrence
T(n)=T(n−1)+T(n−2)+O(1),T(0)=T(1)=O(1)
Each call does a constant amount of work and makes two more calls. The recurrence has the same shape as Fibonacci itself, so the cost grows like the Fibonacci numbers.
15 calls to find fib(5) = 5, but only 6 distinct values exist. The dashed nodes repeat work already done: fib(3) runs twice, fib(2) three times, fib(1) five times.
Tighter than 2ⁿ: the golden ratio
calls(n)=2F(n+1)−1=Θ(φn),φ=21+5≈1.618
fib(5) makes 2⋅8−1=15 calls, fib(20) makes 21,891 and fib(40) about 331 million. The growth rate is φn, which lies between 2n/2≈1.414n and 2n. Saying O(2n) is a correct upper bound; Θ(φn) is the exact rate.
The fix
Memoisation turns it into O(n)
A memoised call
flowchart TD
C["fib(n) is called"] --> Q{"n already in memo?"}
Q -- yes --> H["Return memo[n]: no new calls"]
Q -- no --> B{"n <= 1?"}
B -- yes --> BASE["Return n"]
B -- no --> REC["Compute fib(n-1) + fib(n-2)"]
REC --> S["Store the result in memo[n]"]
S --> RET["Return memo[n]"]
Each value is computed once and looked up every time after that. For fib(5) the second fib(3) and fib(2) become instant hits.
Memoised time is the number of distinct subproblems times the work per subproblem. Naive recursion pays for every path through the tree instead, which is exponential.
Memoised or bottom-up: O(n)
Each fib(k) is computed once and stored.
fib(5) takes 9 calls, fib(40) takes 79.
A bottom-up loop keeps only the last two values: O(1) space.
Naive recursion: O(2ⁿ)
The same values are recomputed again and again.
fib(5) takes 15 calls, fib(40) about 331 million.
The work grows by about 1.6 times for each +1 to n.
Example 3
Combinations: choosing k of n
Choosing 2 of [1, 2, 3, 4] gives 6 combinations: [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]. Backtracking picks a first number and then only looks to its right, so no pair appears twice. For a small fixed k this is polynomial, but over all k, or for k near n/2, the count becomes exponential.
Every subset has some size k, so counting subsets by size adds back up to 2n.
The middle term is exponential on its own
(n/2n)≈πn/22n
The biggest single term carries a large share of all 2n subsets. (1020)=184,756 and (1530)=155,117,520.
Choose
n = 10
n = 20
n = 30
Growth
k=2
45
190
435
O(n2)
k=3
120
1,140
4,060
O(n3)
k=n/2
252
184,756
155,117,520
about 2n/n
any k (all subsets)
1,024
1,048,576
1,073,741,824
2n
Comparison
2ⁿ next to its neighbours
Class
n = 10
n = 20
n = 30
Typical example
O(n2)
100
400
900
Checking every pair
O(n3)
1,000
8,000
27,000
Three nested loops
O(2n)
1,024
about 1 million
about 1 billion
Every subset, naive Fibonacci
O(n!)
3,628,800
about 2.4×1018
about 2.7×1032
Every ordering (permutations)
Exponential beats every polynomial in the long run, and factorial beats exponential.
When 2ⁿ overtakes n²
23=8<9=32,24=16=42,2n>n2 for all n≥5
For tiny inputs the exponential can even be cheaper. From n=5 on it pulls ahead and the ratio 2n/n2 grows without limit.
Taming it
How to escape exponential time
Choosing a technique
flowchart TD
S{"Do branches repeat the same subproblems?"} -- yes --> M["Memoise or bottom-up DP"]
S -- no --> V{"Are the values small, like a target sum?"}
V -- yes --> D["DP over values: O(n x target)"]
V -- no --> P{"Can partial answers be ruled out early?"}
P -- yes --> PR["Backtracking with pruning"]
P -- no --> H{"Is n around 40?"}
H -- yes --> MM["Meet in the middle: about 2^(n/2)"]
H -- no --> E["Accept 2^n for small n, or approximate"]
Memoisation and DP remove the repeats. Pruning and meet in the middle shrink the search but stay exponential.
Memoisation
Cache each subproblem's result. Fibonacci goes from O(2n) to O(n).
Dynamic programming
Build answers from smaller ones. Subset sum becomes O(n⋅target).
Pruning
Stop exploring a branch as soon as it cannot lead to an answer. Faster, still exponential at worst.
Meet in the middle
Split the items in half, enumerate each half, and combine: 2n becomes about 2n/2.
Before and after
Problem
Brute force
Better
Fibonacci
O(2n) naive recursion
O(n) with memoisation
Subset sum
O(2n) try every subset
O(n⋅target) with DP
Two Sum
O(n2) try every pair
O(n) with a hash map
Subset sum, n = 40, huge values
O(2n)
about O(2n/2) meet in the middle
Two Sum's brute force is quadratic, not exponential; it is here because the same idea, remembering what you have seen, fixes it.
Pattern spotting
Spotting O(2ⁿ) in code
Code shape
What it is
Cost
return f(n - 1) + f(n - 2)
Two calls, no cache
O(2n)
return f(n - 1) + f(n - 1)
Two identical calls
Θ(2n); one call doubled is O(n)
skip(i + 1); take(i + 1)
Include or exclude backtracking
O(2n) paths
for mask in range(1 << n)
Every bit mask
O(2n), or O(n⋅2n) with an inner loop
@lru_cache on f(n - 1) + f(n - 2)
Memoised recursion
O(n)
for i ... for j ...
Every pair
O(n2), not exponential
Is this exponential?
flowchart TD
Q1{"Does each call make 2 or more recursive calls?"} -- yes --> Q2{"Does the size shrink by a constant, like n - 1?"}
Q1 -- no --> Q4{"Loop over all 2^n masks or subsets?"}
Q2 -- yes --> Q3{"Are repeated arguments cached?"}
Q2 -- "no, it halves" --> DC["Divide and conquer: often O(n log n)"]
Q3 -- no --> EXP["Exponential: O(2^n) or similar"]
Q3 -- yes --> POLY["Distinct subproblems x work each"]
Q4 -- yes --> EXP
Q4 -- no --> OTHER["Probably polynomial: count the loops"]
Two calls on n−1 or n−2 explode; two calls on n/2 (merge sort) do not, because the depth is only logn.
In the wild
Where exponential time shows up
Subset sum and knapsack
Trying every subset of items to hit a target or fill a bag is the textbook 2n brute force.
N-Queens and Sudoku
Backtracking solvers explore a tree of choices and prune bad branches early.
Brute-force key search
A 128-bit key has 2128≈3.4×1038 values, which is why guessing it is hopeless.
SAT solvers
n true or false variables give 2n assignments; real solvers prune and learn to avoid most of them.
Traveling salesman
Brute force tries every route (n!); Held-Karp DP over subsets brings it to O(n22n).
Feature flag testing
Testing every on/off mix of n flags needs 2n runs, so teams test pairs instead.
Recap
Key takeaways
Key points
O(2ⁿ) means each extra item doubles the work: 2n+1=2⋅2n.
It comes from two choices per item or two recursive calls per call; the choice tree has 2n leaves.
A set of n items has 2n subsets; listing them all costs O(n⋅2n).
Naive Fibonacci follows T(n)=T(n−1)+T(n−2)+O(1) and grows like φn≈1.618n: 15 calls for fib(5).
Memoisation computes each subproblem once, turning Fibonacci into O(n); DP, pruning and meet in the middle tame other cases.
Practical limit: n≈20 to 25. +10 items is ×1,000 the work.