Welcome to the final part of this guide! π
In Part 1, we learned the fundamentals of Time Complexity, Big-O notation, and how to analyze simple loops.
In Part 2, we explored logarithmic complexity (O(log n)), square root complexity (O(βn)), and learned why multiplying or dividing the loop variable changes the complexity dramatically.
Now, it’s time to understand the most common complexities used in real-world algorithms:
- β O(nΒ²)
- β O(n log n)
- β O(nΒ³)
- β Ordering Big-O notations
- β Common interview mistakes
- β Memory tricks
- β Final revision
Let’s begin!
β Example 10 β Nested Loops (O(nΒ²))
One of the most common interview questions involves nested loops.
Consider the following program.
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.println(i + " " + j);
}
}
Step 1 β Analyze the Outer Loop
The outer loop is
for(int i = 0; i < n; i++)
From Part 1, we already know that this loop executes
n
times.
Step 2 β Analyze the Inner Loop
The inner loop is
for(int j = 0; j < n; j++)
This loop also executes
n
times.
Step 3 β Multiply the Iterations
Here’s the important point:
The inner loop runs every time the outer loop executes.
Suppose
n = 4
The execution looks like this.
Outer Loop
β
i = 0
j = 0
j = 1
j = 2
j = 3
i = 1
j = 0
j = 1
j = 2
j = 3
i = 2
j = 0
j = 1
j = 2
j = 3
i = 3
j = 0
j = 1
j = 2
j = 3
The inner loop executes
4 Γ 4 = 16
times.
In general,
n Γ n
which equals
nΒ²
Therefore,
β Time Complexity = O(nΒ²)
π Memory Rule
Whenever one loop is completely inside another,
π Multiply the number of iterations.
β Example 11 β Triangular Loop
Now consider a slightly different nested loop.
for(int i = 0; i < n; i++)
{
for(int j = 0; j < i; j++)
{
System.out.println(j);
}
}
Notice something?
The inner loop no longer executes n times.
Instead,
its execution depends on the value of i.
Dry Run
Suppose
n = 5
The iterations become
i = 0 β 0 iterations
i = 1 β 1 iteration
i = 2 β 2 iterations
i = 3 β 3 iterations
i = 4 β 4 iterations
Total iterations become
0 + 1 + 2 + 3 + 4
Instead of multiplying,
we now need a summation formula.
Summation Formula
The sum
0 + 1 + 2 + ... + (n-1)
is
[
\frac{n(n-1)}{2}
]
Expanding,
[
\frac{n^2-n}{2}
]
Ignoring constants and lower-order terms,
the dominant term is
[
n^2
]
Therefore,
β Time Complexity = O(nΒ²)
π‘ Why Isn’t It O(n)?
Although the inner loop does not always execute n times,
the total number of iterations still grows proportionally to
[
n^2
]
That’s why triangular loops are still O(nΒ²).
β Example 12 β O(n log n)
This complexity is extremely important because many efficient sorting algorithms use it.
Consider the following code.
for(int i = 0; i < n; i++)
{
for(int j = 1; j < n; j = j * 2)
{
System.out.println(i + j);
}
}
Analyze the Outer Loop
for(int i = 0; i < n; i++)
Runs
n
times.
Analyze the Inner Loop
for(int j = 1; j < n; j = j * 2)
This is a logarithmic loop.
It executes
log n
times.
Combine Them
Since the logarithmic loop runs for every outer iteration,
Total operations become
n Γ log n
Therefore,
β Time Complexity = O(n log n)
π Real-World Examples
Algorithms with O(n log n) complexity include:
- Merge Sort
- Heap Sort
- Average Case of Quick Sort
- Many divide-and-conquer algorithms
These are much faster than quadratic algorithms for large inputs.
β Example 13 β O(nΒ³)
Consider the following program.
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n * n; j++)
{
System.out.println(i + j);
}
}
Analyze the Outer Loop
Runs
n
times.
Analyze the Inner Loop
Runs
nΒ²
times.
Multiply
n Γ nΒ²
which equals
nΒ³
Therefore,
β Time Complexity = O(nΒ³)
π Standard Order of Big-O Notations
One of the most frequently asked interview questions is:
Arrange the following Big-O notations from best to worst.
The standard order is:
| Rank | Time Complexity | Name |
|---|---|---|
| π₯ | O(1) | Constant |
| π₯ | O(log n) | Logarithmic |
| π₯ | O(βn) | Square Root |
| 4οΈβ£ | O(n) | Linear |
| 5οΈβ£ | O(n log n) | Linearithmic |
| 6οΈβ£ | O(nΒ²) | Quadratic |
| 7οΈβ£ | O(nΒ³) | Cubic |
| 8οΈβ£ | O(2βΏ) | Exponential |
| 9οΈβ£ | O(n!) | Factorial |
π§ Memory Trick
O(1)
β
O(log n)
β
O(βn)
β
O(n)
β
O(n log n)
β
O(nΒ²)
β
O(nΒ³)
β
O(2βΏ)
β
O(n!)
The higher you are in this list,
the better the algorithm performs as the input grows.
π Why Does This Order Matter?
Suppose
n = 1,000,000
Approximate number of operations:
| Complexity | Approximate Operations |
|---|---|
| O(1) | 1 |
| O(log n) | 20 |
| O(βn) | 1,000 |
| O(n) | 1,000,000 |
| O(n log n) | 20,000,000 |
| O(nΒ²) | 1,000,000,000,000 |
| O(2βΏ) | Practically impossible |
| O(n!) | Astronomically large |
This comparison clearly shows why efficient algorithms are so important.
β οΈ Common Interview Mistakes
Let’s look at the mistakes students frequently make.
β Mistake 1
Thinking
i * 2 < n
is logarithmic.
β Correct Answer:
O(n)
β Mistake 2
Confusing
i += 2
with
i *= 2
Remember:
+2 β O(n)
Γ2 β O(log n)
β Mistake 3
Forgetting that the condition executes
n + 1
times.
Always count the final false condition.
β Mistake 4
Not ignoring constants.
For example,
3n + 2
becomes
O(n)
β Mistake 5
Keeping lower-order terms.
Example
nΒ² + 5n + 100
becomes
O(nΒ²)
π‘ Golden Rules for Calculating Time Complexity
Whenever you see a program,
follow this checklist.
β Step 1
Identify the repeating statement.
β Step 2
Count the number of iterations.
β Step 3
Multiply nested loops.
β Step 4
Use summation formulas for triangular loops.
β Step 5
Ignore constants.
β Step 6
Ignore lower-order terms.
β Step 7
Write the answer using Big-O notation.
π§ Complete Memory Tricks
Constant Increment
i++
i += 2
i += 5
i -= 3
Think
O(n)
Multiplication / Division
i *= 2
i *= 3
i /= 2
Think
O(log n)
Square Root
i * i <= n
or
Math.sqrt(n)
Think
O(βn)
Nested Loops
One loop inside another
β
Multiply
n Γ n
β
O(nΒ²)
Triangular Loops
0 + 1 + 2 + ... + (n-1)
β
Use
[
\frac{n(n-1)}{2}
]
β
O(nΒ²)
π Quick Revision Table
| Java Loop Pattern | Time Complexity |
|---|---|
i++ | O(n) |
i += 2 | O(n) |
i += 3 | O(n) |
i -= 2 | O(n) |
i * 2 < n | O(n) |
i *= 2 | O(log n) |
i /= 2 | O(log n) |
i * i <= n | O(βn) |
| Two Nested Loops | O(nΒ²) |
| Triangular Loop | O(nΒ²) |
| Nested Logarithmic Loop | O(n log n) |
Loop up to n*n | O(nΒ³) |
π― Final Thoughts
Congratulations! π
You have now completed one of the most important topics in Data Structures and Algorithms.
Let’s quickly recap the journey.
In Part 1, you learned:
- β What Time Complexity is
- β What Big-O notation means
- β Why constants are ignored
- β How to analyze simple loops
In Part 2, you learned:
- β Constant increment vs. multiplication
- β Logarithms from scratch
- β Logarithmic loops
- β Square root complexity
- β Prime number optimization
In Part 3, you learned:
- β Nested loops
- β Triangular loops
- β O(n log n)
- β O(nΒ³)
- β Big-O ordering
- β Interview mistakes
- β Memory tricks
π The Most Important Lesson
Don’t try to memorize:
- One loop β O(n)
- Nested loop β O(nΒ²)
- Binary Search β O(log n)
Instead, always ask yourself these questions:
- How many times does the loop execute?
- How does the loop variable change?
- Do I multiply iterations or add them?
- Can I simplify the mathematical expression?
If you answer these questions correctly, you can calculate the Time Complexity of almost any loop-based program.
π What’s Next?
Now that you’ve mastered Time Complexity, you’re ready to move on to more advanced DSA topics, such as:
- π Binary Search
- π¦ Arrays and Strings
- π Linked Lists
- π Stacks and Queues
- π³ Trees
- π Graphs
- β‘ Recursion
- π§© Dynamic Programming
- π Sorting Algorithms
Remember:
Great programmers don’t memorize algorithmsβthey understand how algorithms work.
Keep practicing, analyze every loop you write, and soon calculating Time Complexity will become second nature.
Happy Coding! ππ»