When learning DSA and Big-O, two mathematical concepts appear again and again:
Logarithms and exponentials.
They may sound complicated, but the basic idea is actually simple:
Logarithm asks: “How many times can I divide?”
Exponential asks: “How many times can I multiply?”
Once you understand this, O(log n) and O(2ⁿ) become much easier to understand.
Exponential Growth
Look at this:
2¹ = 2
2² = 4
2³ = 8
2⁴ = 16
2⁵ = 32
2⁶ = 64
2⁷ = 128
Every time n increases by 1, the result doubles.
That's exponential growth.
The general form is:
2ⁿ
For example:
2¹⁰ = 1,024
2²⁰ = 1,048,576
2³⁰ = 1,073,741,824
It grows very quickly.
This is important in DSA because some problems give you multiple choices at every step.
For example:
Take it
OR
Don't take it
One element → 2 choices
Two elements → 4 choices
Three elements → 8 choices
For n elements:
2ⁿ
That's why problems involving subsets, exhaustive choices, and some backtracking can have:
O(2ⁿ)
So, What Is a Logarithm?
A logarithm asks the opposite question.
We know:
2³ = 8
A logarithm asks:
What power of 2 gives me 8?
The answer is:
log₂(8) = 3
So:
2³ = 8
↓
log₂(8) = 3
Logarithm and exponential are inverse concepts.
The easiest way to understand log n
Think about repeatedly dividing by 2.
Start with:
1,000,000
Then:
1,000,000
500,000
250,000
125,000
62,500
...
1
You only need around 20 divisions to go from 1,000,000 to 1.
That's because:
log₂(1,000,000) ≈ 20
This is why logarithmic growth is so useful in algorithms.
Even when n becomes huge, the number of steps grows very slowly.
Binary Search is the perfect example
Imagine you have 1,000,000 sorted numbers and want to find one number.
A normal search could check:
1 → 2 → 3 → 4 → 5 → ...
That's potentially:
O(n)
Binary Search does something different.
It checks the middle and eliminates half of the remaining data:
1,000,000
↓
500,000
↓
250,000
↓
125,000
↓
...
↓
1
Because the search space keeps getting divided by 2:
Binary Search = O(log n)
O(log n) vs O(2ⁿ)
This is where the difference becomes really clear.
| n | log₂(n) |
2ⁿ |
|---|---|---|
| 10 | ~3 | 1,024 |
| 20 | ~4 | 1,048,576 |
| 30 | ~5 | 1,073,741,824 |
| 40 | ~5 | 1,099,511,627,776 |
log n grows very slowly.
2ⁿ grows extremely fast.
That's why:
O(log n)
is commonly seen when we keep reducing the problem.
While:
O(2ⁿ)
often appears when we keep creating new possibilities.
See it directly in code
Logarithmic
let n = 1000000;
while (n > 1) {
n = Math.floor(n / 2);
}
Every iteration cuts the problem in half.
Time: O(log n)
Exponential
function solve(n) {
if (n === 0) return;
solve(n - 1);
solve(n - 1);
}
Each function call creates two more calls.
The number of calls keeps multiplying:
solve
/ \
solve solve
/ \ / \
... ... ... ...
So the complexity is approximately:
O(2ⁿ)
The DSA connection
You don't need to remember complicated mathematical definitions.
Just recognize the pattern.
If you see:
Repeatedly dividing the problem
Think:
LOGARITHM → O(log n)
If you see:
Multiple choices branching from each element
Think:
EXPONENTIAL → O(2ⁿ)
The mental model
Remember these two lines:
LOGARITHM
“How many times can I divide?”
EXPONENTIAL
“How many times can I multiply?”
Or even simpler:
Halving →
log n
Branching →2ⁿ
That's the connection between logarithms, exponentials, and Big-O.
Once you start seeing halving and branching in DSA problems, these complexities become much easier to recognize.

Top comments (0)