Finding the Injection for Anesthesia (GeeksForGeeks) - BitMask solution
Original problem link: https://www.geeksforgeeks.org/puzzle-5-finding-the-injection-for-anesthesia/
Question:
In a Medical Laboratory, you have 240 Injections, one of which is for Anesthesia for a rat. If Anesthesia injection is injected in a rat, it will faint in exactly 24 hours. You have 5 rats available to determine which one injection contains the Anesthesia. How do you achieve this in 48 hours ?
Solution:
Given, we have 5 rats only,
Total injections: 240
1 is real
Time available: 48 hrs
Now, consider 5 rats as bits of length=5, by this way, we can represent a number from 0 to 31
Eg. 0 -> 0 0 0 0 0
1-> 0 0 0 0 1
.....
31->1 1 1 1 1
Where '1' at a position represents that a particular rat has been affected by real injection.
So,
We will divide all injections into a group of 31 numbered as (0 to 31) .
At 0th hour we'll inject all rats which will have 1 in this place to make that particular injection number on injection.
Eg. for injection no. 11, we get (01011) as binary, so the 2nd,4th, and 5th rat will be given the injection.
Similarly, at 1st hour, we'll give another batch of (0 - 31 ) numbered injections.
In this way, we can inject all injections within 24 hours as (24*32=748) which is a much larger number than required.
As it is given in the question, we can see the effect of injection in exactly 24 hrs, so if on 2nd day at 3rd hour,
rat no. 2,3,4 die then we get binary (01110) which will be equivalent to 14 in decimal, that mean 24 hour before's batch of injection no. 14 was real.
Comment, if there is any doubt.