🧠 Why Writing 100 Lines of Code Can Be Better Than Just 2 – A Gentle Introduction to Time Complexity


☕ Ever written a beautifully short 2-line solution that somehow took forever to run?
Yeah, me too.
Sometimes, code that looks elegant… performs terribly.
This is where time complexity comes into play — the hidden superpower that turns "more lines" into "more speed."
In this blog, we’ll explore why writing a bit more code can sometimes make a huge difference in performance — and how understanding Big O notation can help you become a smarter problem solver.
📏 Code Length ≠ Performance
Writing less code doesn’t always mean writing better code.
A 2-line brute-force solution might feel satisfying, but if it runs in O(n²)
time, it's going to struggle on large inputs.
A slightly longer solution — say 8–10 lines — that uses O(n)
time will run circles around it.
📚 What is Time Complexity?
Time complexity helps us estimate how fast a program runs as the input size increases.
We use something called Big O notation to express it:
🟢
O(1)
→ Constant time (fastest)🟡
O(n)
→ Linear time🔴
O(n²)
→ Quadratic time (slows down fast!)
It doesn’t measure exact seconds, but it shows how your algorithm scales with bigger inputs.
🧪 Real-World Example – Finding Duplicates
Let’s say you want to check if an array has duplicates.
👎 Brute Force – Short but Slow (O(n²)
)
cppCopyEditfor(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++)
if(arr[i] == arr[j]) return true;
👍 Hashing – Longer but Faster (O(n)
)
cppCopyEditunordered_set<int> s;
for(int num : arr) {
if(s.count(num)) return true;
s.insert(num);
}
The first one looks nice and short — but dies on large inputs.
The second one? Slightly longer, but far more efficient.
🧠 Why Long Code is Sometimes Better
Writing longer code can be a sign that:
You're handling edge cases better
You’ve optimized for speed and memory
Your logic is clearer and easier to debug
So don’t fall in love with "shortest possible solution."
Fall in love with readable, reliable, and fast code.
🧾 Quick Time Complexity Cheatsheet
Pattern | Time Complexity |
Single loop | O(n) |
Nested loops | O(n²) |
Binary search | O(log n) |
Hashing lookup | O(1) avg. |
Merge sort | O(n log n) |
Recursion (Fibonacci naive) | O(2^n) |
✅ Conclusion
“Don’t fear writing more code. Fear writing inefficient code.”
Whether you're solving Leetcode problems or building real-world software, performance matters — not just brevity.
💬 Got your own example of short code vs. smart code?
Let’s discuss in the comments 👇
Or tag me on X @yourhandle — I’d love to learn with you!
🔁 Before You Go…
I'm just getting started with sharing what I learn — one step, one bug, one blog at a time.
If you found this helpful (or spotted something off 👀), feel free to drop a thought below.
Let’s grow together 🚀
Subscribe to my newsletter
Read articles from Jameel Askeri Askeri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
