You multiply them only when one loop is nested completely inside the body of another loop.
Analyzing Time Complexity of Multiple Consecutive Loops
When do you multiply loop complexities?
The answer
More FAQs in Analyzing Time Complexity of Multiple Consecutive Loops
You add their complexities together. O(N) + O(N) = O(2N), which is simplified by dropping the constant to O(N).
If Loop A iterates over array size N, and Loop B iterates over array size M, the complexity is O(N + M). You cannot drop one because you don't know which is larger.
No, adding 5 loops makes it O(5N). Dropping the constant makes it O(N). You only get exponents (like N^2) by nesting loops.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.