DEV Community

Jayy Prajapat
Jayy Prajapat

Posted on

Logarithms vs Exponentials: The Simple Idea Behind O(log n) and O(2ⁿ)

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
Enter fullscreen mode Exit fullscreen mode

Every time n increases by 1, the result doubles.

That's exponential growth.

The general form is:

2ⁿ
Enter fullscreen mode Exit fullscreen mode

For example:

2¹⁰ = 1,024
2²⁰ = 1,048,576
2³⁰ = 1,073,741,824
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

One element → 2 choices
Two elements → 4 choices
Three elements → 8 choices

For n elements:

2ⁿ
Enter fullscreen mode Exit fullscreen mode

That's why problems involving subsets, exhaustive choices, and some backtracking can have:

O(2ⁿ)
Enter fullscreen mode Exit fullscreen mode

So, What Is a Logarithm?

A logarithm asks the opposite question.

We know:

2³ = 8
Enter fullscreen mode Exit fullscreen mode

A logarithm asks:

What power of 2 gives me 8?

The answer is:

log₂(8) = 3
Enter fullscreen mode Exit fullscreen mode

So:

2³ = 8
      ↓
log₂(8) = 3
Enter fullscreen mode Exit fullscreen mode

Logarithm and exponential are inverse concepts.


The easiest way to understand log n

Think about repeatedly dividing by 2.

Start with:

1,000,000
Enter fullscreen mode Exit fullscreen mode

Then:

1,000,000
500,000
250,000
125,000
62,500
...
1
Enter fullscreen mode Exit fullscreen mode

You only need around 20 divisions to go from 1,000,000 to 1.

That's because:

log₂(1,000,000) ≈ 20
Enter fullscreen mode Exit fullscreen mode

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 → ...
Enter fullscreen mode Exit fullscreen mode

That's potentially:

O(n)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Because the search space keeps getting divided by 2:

Binary Search = O(log n)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

is commonly seen when we keep reducing the problem.

While:

O(2ⁿ)
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Every iteration cuts the problem in half.

Time: O(log n)
Enter fullscreen mode Exit fullscreen mode

Exponential

function solve(n) {
  if (n === 0) return;

  solve(n - 1);
  solve(n - 1);
}
Enter fullscreen mode Exit fullscreen mode

Each function call creates two more calls.

The number of calls keeps multiplying:

             solve
            /     \
        solve     solve
        /  \       /  \
      ...  ...   ...  ...
Enter fullscreen mode Exit fullscreen mode

So the complexity is approximately:

O(2ⁿ)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

If you see:

Multiple choices branching from each element

Think:

EXPONENTIAL → O(2ⁿ)
Enter fullscreen mode Exit fullscreen mode

The mental model

Remember these two lines:

LOGARITHM
“How many times can I divide?”
Enter fullscreen mode Exit fullscreen mode
EXPONENTIAL
“How many times can I multiply?”
Enter fullscreen mode Exit fullscreen mode

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)