🕒 Time Complexity: Why Fewer Lines Don't Always Mean Faster Code

👋 Introduction

As a college student just starting my journey in Data Structures and Algorithms (DSA), I used to believe that the fewer lines of code I wrote, the better my program was. But then something clicked — during one of our DSA lectures, our professor gave us a brain-teasing problem involving 9 balls. That example changed the way I look at time complexity forever.

In this blog, I’ll share what I learned about time complexity using real-life examples, including the famous 9 balls problem and more creative ones. If you're someone just getting started with DSA, this post will help you understand why time complexity matters more than code length.


🎯 The 9 Balls Problem—Brute Force vs Divide and Conquer

You have 9 balls. 8 of them weigh the same, and 1 ball is heavier. You must find the heavy one using the fewest comparisons.

❌ Approach 1: Brute Force

Compare each ball one by one, and store the maximum weight.

  • Time Complexity: O(n)

  • Simple logic, but slow

  • 9 comparisons in worst case

✅ Approach 2: Divide into Groups

Split into 3 groups of 3 (A, B, C). Weigh A vs. B:

  • If A == B, heavier ball is in C.

  • If A > B, then heavier is in A.

  • If A < B, then heavier is in B.

Repeat this process. In just 2 comparisons, you find the answer!

💡 Even though the second method might require slightly more thinking or lines to implement, it is way faster as the input grows.


🧠 Example 2: Finding a Duplicate Number

❌ Brute Force: Compare Every Pair (O(n²))

pythonCopyEditfor i in range(len(arr)):
    for j in range(i+1, len(arr)):
        if arr[i] == arr[j]:
            return arr[i]

Simple, right? But very inefficient for large lists.

✅ Optimized: Floyd’s Cycle Detection (O(n))

pythonCopyEditslow = fast = arr[0]
while True:
    slow = arr[slow]
    fast = arr[arr[fast]]
    if slow == fast:
        break

fast = arr[0]
while fast != slow:
    slow = arr[slow]
    fast = arr[fast]

return fast

More lines of code — but drastically faster.


🔍 Example 3: Prime Number Check

❌ Naive Approach (O(n))

pythonCopyEditdef is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

✅ Optimized: Check till √n (O(√n))

pythonCopyEditimport math
def is_prime(n):
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

Less time, more performance, better scalability.


🔄 Example 4: Sorting an Array

❌ Bubble Sort (O(n²))

pythonCopyEditfor i in range(len(arr)):
    for j in range(len(arr)-i-1):
        if arr[j] > arr[j+1]:
            arr[j], arr[j+1] = arr[j+1], arr[j]

Classic, but slow.

✅ Built-in Sort (TimSort, O(n log n))

pythonCopyEditarr.sort()

Just one line — but under the hood, it’s a highly optimized algorithm.


🚀 Key Takeaway

Time complexity is not about how many lines of code you write — it's about how smartly your algorithm handles growth.

You may write 10 lines that run in 1 second or 2 lines that run in 10 minutes. What matters is not the number of lines but the number of operations your code performs.


🧩 Final Thoughts

As beginners, it’s easy to fall into the trap of valuing short code over smart code. But as you grow in DSA, you'll realize efficiency beats brevity.

Keep learning, keep questioning, and most importantly — start caring about how your code scales. That’s what turns a good coder into a great problem solver. @chaicode

0
Subscribe to my newsletter

Read articles from Hemant Nirmalkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hemant Nirmalkar
Hemant Nirmalkar