Practice Discrete Math

Algorithms / Loop Analysis

Least You Need to Know: Analyzing Loops and Nested Loops

For loop analysis, count how many times the body executes and multiply by the work per execution. Nested loops often multiply counts, while doubling or halving patterns often lead to logarithms.

The least you need to know

Key notation

for i in range(n) about n iterations
∑_{i=1}^n i triangular count
i ← 2i doubling step

Tiny worked example

  • A single pass over an array of length `n` with constant work per element uses `Θ(n)` time.
  • A nested pass over every ordered pair of elements uses about `n^2` body executions.
  • A doubling loop reaches `n` after about `log_2 n` updates.

Common mistakes

How to recognize this kind of problem

Next recommended lesson

Continue through this topic with Least You Need to Know: Modular Arithmetic, Divisibility, and Hashing Intuition.

Least You Need to Know: Modular Arithmetic, Divisibility, and Hashing Intuition

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

Start practice