Why is 100 lines of code is sometimes better than two lines of code?

The best example is Find the Fake Coin :- You are given 12 coins. One of them is fake and lighter than the others and you're allowed only 3 times. Find the fake coin.
You check each coin individually.
For example:
for (i in coins) if (coins[i] != 1) return i;
Time complexity: worst case: check all 12 coins
Simple code, but inefficient for larger problems or constrained situations.
This approach is Brute Force approach within 2-line idea.
You're allowed only 3 attempts, so your approach should follow this constraint.
Step 1: Divide into 3 groups of 4 coins each.
Let’s say:
Group A = coins [0,1,2,3]
Group B = coins [4,5,6,7]
Group C = coins [8,9,10,11]
Step 2: Compare A vs B on the balance scale.
If A == B → fake coin is in Group C
If A > B → fake coin is in B (lighter group)
If A < B → fake coin is in A (lighter group)
Step 3: You now have 4 coins.
Repeat the same process by dividing the 4 coins further and comparing.
Within 3 comparisons, you can confidently find the fake coin.
The above approach is better than the brute-force approach because it uses an optimized divide and conquer strategy.
Subscribe to my newsletter
Read articles from satyasandhya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
