It’s a question to find an algorithm that gives the highest floor where you can drop a water balloon without breaking it at a 100 story building. You have only two balloons. I had actually seen this problem before and solved it already. It was not balloons but eggs. The solution is simple. Since if you miss two chances then have no balloons available, you need to try from the lowest safe level which means throw it from the first floor then if it does not pop, pick it up and go up to the level 2 and repeat it until the balloon pops. When you get the level from which the balloon bursts after dropping it, one level below this level is the highest safe level. (e.g. If the balloon first breaks on the 20th floor, the highest safe floor is the 19th).

It’s easy isn’t it? Yet the post above has one more condition to make it a little bit more difficult which is that you can try only 15 times. This means if I try the solution I’ve just described, I can test up to only the 15th floor. If the breaking point is the 40th floor or even the 16th floor, I’ll never know it. When I first saw it, I tried to solve it for about five minutes yet couldn’t solve it. That time, I was too busy to think about it further thus couldn’t be bothered to do it. So I saved the URI of the post and forgot about it. Last night, no actually today morning, at approximately 6 am, I went to bed (yeah, am I a vampire or what? :)). Although I didn’t even try to think about the problem yet all of sudden I came up with the answer of that question. It was just like the ‘Eureka‘ story of Archimedes. Just like he figured out how to assess the purity of a golden crown when he stepped into a bath. OK, stop flattering myself I figured it out as the question was easy to solve.

Well, an important point here is that I in fact often find solutions to some difficult problems when I am not struggling to solve it yet am doing something irrelevant to the problem such as working out at the gym, washing dishes after a meal, lying on the bed to sleep or even when I’m dreaming. So if you can’t solve some problem then why don’t you try something else rather than thinking about the solution all the time. Your brain may need to relax or when you use the other parts of the brain by doing something else, it might figure it out.

Anyway, here is what my brain told me that time.

Since I have only two balloons, if I choose a wrong floor twice, I lose all the chances. Then what if I check every second level as I have two balloons. So first, if I drop it from the second level and it breaks then I still have one more balloon so that I can try it from the first level and I can find if the highest safe level is the first or there is no safe level. If the balloon is fine after dropping it from the second level then I can pick it up and try it from the fourth level and so on. However, with this solution I can check at most until the 30th floor which is better than checking up to the 15th one but still not enough to check the 100 stories. This means that it is not sufficient to check ever second level in order to find the floor and I should focus on not only the two balloons but also the 15 times of attempts. From the first solution above, I know that I can definitely have 15 times if I try from the lowest safe floor and move up. It means if the number of floors between the already known highest safe level and the breaking point level found is the same as or fewer than the number of attempts I still have, I can find the very first level where the balloon bursts after dropping it (In order to reduce the number of attempts, of course, I have to make it the same as the number of attempts still left). Therefore I can tell the highest safe level that is one level below the breaking point level found. This should be easier to understand with an example.

Let’s assume that the breaking point is the 17th floor.

At first, I have 15 tries to drop one balloon from the 15th floor.
The balloon’s still OK and I now have 14 times of attempts. Since the number of attempts I still have is 14 time, I can’t try it from the 30th floor which is 15 + 15 yet I have to try it from the 29th floor (15 + 14). Because the breaking point is the 17th floor, the balloon breaks on the 29th floor. Now I have 13 times of attempts and 1 balloon left. From the first try, I know the 14th floor is fine so I try from the next floor that is floor 15 to see if floor 14 is the highest safe floor. It turns out the 15th one is fine and now I have 12 times left. 16th -> OK / 11 times left, 17th -> it breaks! / 10 times left
So I can tell the 16th floor is the highest safe floor and I didn’t even use all the 15 times of attempts.

The post that has the question specifies the floor starts from 0 so let’s make the first floor is floor 0 and see have it works. I made a table which shows the number of attempts required to find the highest safe floor.

So if the highest safe floor is 73. The first balloon breaks on the floor 74 after 6 times of attempts. After that try from the floor 65 (attempts: 7) through 66 (8), 67 (9), 68 (10), 69 (11), 70 (12), 71 (13), 72 (14) to 73 (15) and the second balloon hasn’t broken so I can tell the highest safe floor is the floor 73.

I am sure that the findHighestSafeFloor() method above works but I decided to make it recursion. Why? No special reasons. I just like recursion as I haven’t used it for approximately four years since I last programmed in a functional programming language like Haskell in my math class thus miss it.

Although the code can be very simple as the following code with recursion, it has to increase the number of attempts and to decrease the number of balloons left, so it would be a bit more verbose as the second code.

Well, the code looks a bit ugly but in my defence, I’d just like to see the result and am busy so had no stomach for making it pretty. (Oh! such a poor excuse / but really busy :()

The dropBalloon() and the breakBalloon() are added to increase the number of attempts made and to decrease the number of balloons left respectively. The dropBalloon() method also checks if the number of attempts already made is 15 and it tries to make another one than throws an exception telling that all the chances have been used. Similarly the breakBalloon() method checks if all the available balloons have broken if so, it also throws an exception saying there are no available balloons left. Thus I can see if the programme can find the highest safe floor with 15 times or fewer times of attempts and only two water balloons. If it cannot, it immediately throws an exception and stop the programme so I can see my programme fails to prove my algorithm.

The init() method is to initialise all the variables so that I can test it many times. It checks all the floors from the first one (floor 0) to the last one (floor 99) so if the programme works fine without any exception thrown then it might prove my answer is correct.