# Red and Blue Balls in a Bag

Problem:

You have 20 Blue balls and 10 Red balls in a bag. You put your hand in the bag and take off two at a time. If they’re of the same color, you add a Blue ball to the bag. If they’re of different colors, you add a Red ball to the bag. What will be the color of the last ball left in the bag?

Note: Assume you have a big supply of Blue and Red balls for this purpose. When you take the two balls out, you don’t put them back in, so the number of balls in the bag keeps decreasing.

Once you tackle that, what if there are 20 blue balls and 11 red balls to start with?

Solution:

There can be 3 possible cases taking off 2 balls from bag.
a) If we take off 1 Red and 1 Blue, in fact we will take off 1 Blue
b) If we take off 2 Red, in fact we will take off 2 Red (and add 1 Blue)
c) If we take off 2 Blue, in fact we will take off 1 Blue

So In case of (a) or (c), we are only take off one Blue ball. Also, we always take off Red balls two by two.

1) 20 Blue, 10 Red balls
If there are 10 (even) number of Red balls, we can not have one single Red ball left in the bag, so the last ball will be Blue.

2) 20 Blue, 11 Red balls
Now as the no. of Red balls is odd, there will be one single Red ball in the bag with other Blue balls, and whenever we remove 1 Red and 1 Blue ball, we end up taking off only the Blue ball. So the Red ball will be the last ball in the bag.

### 5 Thoughts on “Red and Blue Balls in a Bag”

1. Prakash on November 4, 2013 at 6:35 pm said:

Google Code Jam Question……. Is it

2. Lauren on July 29, 2015 at 9:04 pm said:

Going back to the first case, with 20 blue balls and 10 red balls:

Let’s say you pick all blue balls first, and replace with 10 blue balls.
You then pick the 10 blue balls left and replace with 5 blue balls.
You then pick 2 more pairs of blue balls, and are left with 3 blue balls.
You then pick 2 of the 3 remaining blue balls and are left with a single pair of blue balls.

Now, for the reds…
Just as with the blue, you pick all 10 red balls, and replace with 5 blue (because they are the same color).
You then pick 2 pairs of red balls, and are left with 3 blue balls.
You then pick 2 of the remaining red balls, and are left with 2 red balls.
You choose the pair of red balls next, and are left with a single red ball.

So, we have a case where we are left with 2 blue balls and 1 red balls.

There are two cases.
First, we could pick the 2 blue balls. We would replace with another blue ball.
We now have 1 red ball and 1 blue ball. This would be our final pair, which would be replaced by a red ball.

Next, we could pick 1 blue ball and 1 red ball. We would replace this pair with 1 red ball. Thus, we would be left with 1 red ball and 1 blue ball. This would be the final pair, and we would replace it by a red ball.

Thus, I believe this answer is incorrect. It IS possible to end with a red ball when you start with 20 blue balls and 10 red balls.