Time Complexity of two for loops
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Two for loops do not automatically mean O(n^2). The complexity depends on whether the loops run one after another, whether one loop is inside the other, and whether the inner loop bound changes with the outer loop index. Good analysis starts by counting how many total iterations actually happen.
Sequential Loops Add, They Do Not Multiply
If two loops run one after the other, their costs add.
The first loop runs n times and the second loop also runs n times. Total work is about 2n, which simplifies to O(n).
That is the first common mistake: seeing two loops and assuming quadratic time even when they are not nested.
Fully Nested Loops Usually Multiply
If the inner loop runs n times for each of n outer iterations, the total is n * n.
The inner statement runs n^2 times, so this is O(n^2).
More generally:
- outer loop
a(n)times - inner loop
b(n)times per outer iteration - total
a(n) * b(n)
Variable Inner Bounds Change the Math
Not every nested loop is quadratic. Consider:
Here the inner loop runs:
- '
0times wheni = 0' - '
1time wheni = 1' - '
2times wheni = 2' - and so on
So total work is:
0 + 1 + 2 + ... + (n - 1)
That arithmetic series sums to about n^2 / 2, which is still O(n^2) in Big O terms.
The coefficient changed, but the growth class did not.
Some Nested Loops Are Not Quadratic
The inner loop may shrink enough that the complexity changes.
The outer loop runs n times. The inner while loop runs about log n times. Total complexity is O(n log n).
So the real question is never "how many loops are there?" It is "how many times does the body execute as input grows?"
Ignore Constants, Keep Growth
Big O drops constants and lower-order terms. That is why:
- '
O(2n)becomesO(n)' - '
O(n^2 / 2)becomesO(n^2)' - '
O(n log n + n)becomesO(n log n)'
This does not mean constants are irrelevant in practice. It means they do not change the asymptotic growth class.
Count the Dominant Operation
When analyzing code, identify the operation that dominates runtime. For example:
This function has one O(n) loop and one O(n^2) nested loop. The total is O(n + n^2), which simplifies to O(n^2) because the quadratic term dominates as n grows.
Common Pitfalls
- Assuming any two loops automatically mean
O(n^2). - Forgetting that sequential loops add rather than multiply.
- Ignoring changing inner bounds such as
range(i)orwhile j > 1. - Counting only loop syntax instead of counting actual body executions.
- Treating Big O as exact runtime instead of asymptotic growth.
Summary
- Two sequential loops are usually
O(n), notO(n^2). - Fully nested
nbynloops are typicallyO(n^2). - Changing inner bounds can still be quadratic or can become
O(n log n)or something else. - The correct method is to count how many times the inner work runs.
- Big O is about growth rate, not exact elapsed time.
Related reading
- Time complexity to generate all pairs in an array
- Time/Space Complexity of Depth First Search
- Tinyurl-style unique code potential algorithm to prevent collisions
- Tips implementing permutation algorithm in Java
- Time cost of training with pytorch DDP with multi-GPUs
- Timed annotation in spring metrics
- To make a distance matrix or to repeatedly calculate distance
- To print the boundary of Binary Tree

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.