Tuesday, 19 Oct 2021

# Minimum number of pigs required to find the poisonous bucket

Given an integer N denoting the number of buckets, and an integer M, denoting the minimum time in minutes required by a pig to die after drinking poison, the task is to find the minimum number of pigs required to figure out which bucket is poisonous within P minutes, if there is exactly one bucket with poison, while the rest is filled with water.
Examples:

Input: N = 1000, M = 15, P = 60Output: 5Explanation: Minimum number of pigs required to find the poisonous bucket is 5.
Input: N = 4, M = 15, P = 15Output: 2Explanation: Minimum number of pigs required to find the poisonous bucket is 2.

Approach: The given problem can be solved using the given observations:A pig can be allowed to drink simultaneously on as many buckets as one would like, and the feeding takes no time.
After a pig has instantly finished drinking buckets, there has to be a cool downtime of M minutes. During this time, only observation is allowed and no feedings at all.
Any given bucket can be sampled an infinite number of times (by an unlimited number of pigs).
Now, P minutes to test and M minutes to die simply tells how many rounds the pigs can be used, i.e., how many times a pig can eat. Therefore, declare a variable called r = P(Minutes To Test) / M(Minutes To Die).
Consider the cases to understand the approach:
Case 1: If r = 1, i.e., the number of rounds is 1.Example: 4 buckets, 15 minutes to die, and 15 minutes to test. The answer is 2. Suppose A and B represent 2 pigs, then the cases are:

Obviously, using the binary form to represent the solution as:

Conclusion: If there are x pigs, they can represent (encode) 2x buckets.
Case 2: If r > 1, i.e. the number of rounds is more than 1. Let below be the following notations:
0 means the pig does not drink and die.
1 means the pig drinks in the first (and only) round.
Generalizing the above results(t means the pig drinks in the t round and die): If there are t attempts, a (t + 1)-based number is used to represent (encode) the buckets. (That’s also why the first conclusion uses the 2-based number)
Example: 8 buckets, 15 buckets to die, and 40 buckets to test. Now, there are 2 (= (40/15).floor) attempts, as a result, 3-based number is used to encode the buckets. The minimum number of pigs required are 2 (= Math.log(8, 3).ceil).

Below is the implementation of the above approach:

C++

#include
using namespace std;

void poorPigs(int buckets,
int minutesToDie,
int minutesToTest)
{

cout