Skip to main content

How 25 Lines of Code Can Be Better Than 2 Lines

Updated 3 min readMarkdown

When it comes to programming, we often hear that “less is more.” Concise code is usually praised for being elegant and easy to maintain. But is shorter code always better? Sometimes, a few extra lines can make your code much more efficient-especially when tackling algorithmic problems. Let’s explore this idea with a classic puzzle: finding the heavier ball among nine, using the fewest possible steps.

The Problem

Imagine you have an array of 9 balls. Eight balls are identical in weight, but one is heavier. Your task is to identify the heavy ball in the minimum number of steps.

Approach 1: The Brute Force Way

The most straightforward solution is to loop through the array, comparing each ball’s weight to find the heaviest. Here’s what that looks like in code:

int weight = 0;
for(int i = 0; i < 9; i++){
    if(ball[i] > weight){
        weight = ball[i];
    }
}

This approach is simple and only takes a couple of lines. However, it requires up to 8 comparisons-one for each ball after the first.

Approach 2: Divide and Conquer

Now, let’s get a bit more creative. What if we could eliminate most of the balls with just a couple of comparisons? Here’s how:

  1. Divide the 9 balls into 3 groups of 3:

    • Group A: balls 0, 1, 2

    • Group B: balls 3, 4, 5

    • Group C: balls 6, 7, 8

  2. Compare the total weight of Group A and Group B:

    • If A == B, the heavy ball is in Group C.

    • If A > B, the heavy ball is in Group A.

    • If A < B, the heavy ball is in Group B.

  3. Now, you’re left with 3 balls. Compare two of them:

    • If the first two are equal, the third is the heavy one.

    • Otherwise, the heavier one between the two is your answer.

Here’s what the code might look like:

int a[9] = {1,1,1,2,1,1,1,1,1};
int A = a[0] + a[1] + a[2];
int B = a[3] + a[4] + a[5];
int C = a[6] + a[7] + a[8];

int heavy;
if (A == B) {
    // Heavy ball is in Group C
    if (a[6] > a[7])
        heavy = 6;
    else if (a[7] > a[6])
        heavy = 7;
    else
        heavy = 8;
} else if (A > B) {
    // Heavy ball is in Group A
    if (a[0] > a[1])
        heavy = 0;
    else if (a[1] > a[0])
        heavy = 1;
    else
        heavy = 2;
} else {
    // Heavy ball is in Group B
    if (a[3] > a[4])
        heavy = 3;
    else if (a[4] > a[3])
        heavy = 4;
    else
        heavy = 5;
}

Why More Lines Can Be Better

At first glance, the second approach seems more complicated-it takes more lines of code and a bit more thought. But here’s the magic: you only need 2 comparisons to find the heavy ball, instead of 8! With each comparison, you eliminate two-thirds of the possibilities. That’s the power of divide and conquer.

The Takeaway

Short code isn’t always the smartest code. Sometimes, a few extra lines can make your solution dramatically more efficient. Next time you’re faced with a tricky problem, don’t be afraid to write a little more code if it helps you solve the problem faster and smarter.

Efficiency isn’t just about writing less-it’s about doing more with what you write.

No comments yet. Be the first to comment.