1

Given an array of rubber balls, for example: all balls are equal weight except one. What is the most efficient way to find the ball that has the unique weight, and that would require the least amount of placing the balls on a scale?

3
  • I'm 95% sure that this question has already been asked over on Puzzling Commented Apr 3, 2015 at 17:26
  • 1
    What have you done so far to try to solve this problem and where are you currently stuck? This sounds a lot like a homework problem (see StackOverflow guidelines on homework questions: stackoverflow.com/help/on-topic) Commented Apr 3, 2015 at 17:32
  • actually, I'm 39 years old, and I saw it on Brooklyn 9 9. I was just wondering if anyone had a solution better than 0(n) Commented Apr 3, 2015 at 18:34

3 Answers 3

1

You can easily do it via Binary Search Algorithm which is O(logn). You simply divide the group of balls into two equal groups. Then pick a side and divide those. Weigh them. If the piles are equal, the ball is in the other pile. If they are unequal, you continue the process on these two piles. Pick one, split and weigh. You will eventually isolate the ball that is different.

Sign up to request clarification or add additional context in comments.

4 Comments

Valid, if suboptimal.
I wasn't going for optimal, I was trying to beat his O(n) requirement which I was pointing out is VERY easy to do.
Regrettably, least amount of placing the balls on a scale leaves to interpretation whether this is least total ball count , least number of weighings , least number of brain cells involved in figuring out a procedure , … I wasn't going for optimal So, what do you make of most efficient way to find the ball that has the unique weight, and that would require the least amount of placing the balls on a scale??
he clarified in his comment: "actually, I'm 39 years old, and I saw it on Brooklyn 9 9. I was just wondering if anyone had a solution better than 0(n)" ... So that would be what I was going for, A down and dirty, quick solution that beat O(n)... You can always feel free to provide your own, more efficient, solution rather than trolling the valid solutions provided by others.
0

I believe you meant you can only use a scale to know the distinct ball. The following is my solution.

You have 3 cases:

Case 0: if there is only `1` ball, then this is the desired ball.

Case 1: the number of balls is divisible by 3. 
        Divide the balls into 3 equal sets {1, 2, 3}.
        weigh 1 and 2 ==> if equal, recurse on 3
                          if not, weigh 1 and 3 ==> if equal recurse on 2
                                                    if not recurse on 1




Case 2: the number of balls leave a remainder of 1 when divided by 3. 
        Take one ball out, call that ball x.
        You want to check if x is the desired ball or not.
        Take 2 more balls, call them y and z.

        Weigh y and z ==> if equal, weigh x and y ==> if equal, x is not the desired ball. This is case 0 or 1 on the set of balls without x.
                                                      if not, x is the distinct ball
                          if not, weigh x and y ==> if equal, z is the distinct ball
                                                    if not, y is the distinct ball.



Case 3: the number of balls leave a remainder of 2 when divided by 3.
        Take two balls out, call them x and y.
        Take one more ball, call it z.

        weigh x and y ==> if equal, weigh x and z ==> if equal this is case 0 or 1 on the remaining balls without x and y.
                                                      if not, z is the distinct ball.
                          if not, weigh x and z ==> if equal, y is the distinct ball.
                                                    if not, x is the distinct ball.

Comments

0

Let's do it for 8 balls:

Leave 2 out, and weigh 3 - 3: you know which group of 3 has the different ball, or you know that the one left out is different, in which case one more weighting will find it.

To find it from the 3, leave one out and weigh them 1 - 1. Again, you will find it no matter what, and we did it in 2 weightings, beating the binary search.

For 16, you can do it in 3:

Leave 6 out. Compare 5 - 5. If equal, we can find it from the ones left out in 2 weightings.

From the 5, we can find it by comparing 2 - 2 then 1 - 1.

In general, if we have 3^x <= n < 3^(x + 1) balls, then we can do it in x + 1, which I believe is optimal, but don't have a proof:

3^1 <= 8 < 3^2 => answer = 2
3^2 <= 16 < 3^3 => answer = 3

This is because we can always split the number of balls into 3 groups of size k, k, k; k, k, k + 1 or k, k + 1, k + 1, which can then be solved recursively faster than if you split it into just two halves.

I'm not sure if this is optimal, but it beats the classic binary search.

1 Comment

This solution is valid but I am not totally convinced of its ability to take down binary search in the worst case. Certainly in best case it does. However, I think in your examples you are making some assumptions. most importantly of which is that you automatically know which group has the different ball. All you know by weighing against each other is that there is a different ball in ONE of these groups. Since you do not know if the different ball is heavier or lighter, you would need a reference weighing on one of those groups. This changes your worst case significantly, i think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.