Thursday, April 26, 2007

One odd in 12 balls

The Problem:

You have 12 balls identical in size and appearance but 1 is an odd weight (could be either light or heavy).

You have a set scales (balance) which will give 3 possible readings: Left = Right, Left > Right or Left < Right (ie Left and Right have equal weight, Left is Heavier, or Left is Lighter).

You have only 3 chances to weigh the balls in any combination using the scales. Determine which ball is the odd one and if it's heavier or lighter than the rest. How do you do it?

The Solution:

Number the balls 1, 2, 3, ... 10, 11, 12

Start off with them in 3 groups: [1, 2, 3 and 4], [5, 6, 7 and 8] and [9,10,11 and 12]

Weigh 1, 2, 3 and 4 vs 5, 6, 7 and 8 with 3 possible outcomes:

1. If they balance then 9,10,11,12 have the odd ball, so weigh 6,7,8 vs 9,10,11 with 3 possible outcomes:

1a If 6,7,8 vs 9,10,11 balances, 12 is the odd ball. Weigh it against any other ball to determine if heavy or light.

1b If 9,10,11 is heavy then they contain a heavy ball. Weigh 9 vs 10, if balanced then 11 is the odd heavy ball, else the heavier of 9 or 10 is the odd heavy ball.

1b If 9,10,11 is light then they contain a light ball. Weigh 9 vs 10, if balanced then 11 is the odd light ball, else the lighter of 9 or 10 is the odd light ball.



2. If 5,6,7,8 > 1,2,3,4 then either 5,6,7,8 contains a heavy ball or 1,2,3,4 contains a light ball so weigh 1,2,5 vs 3,6,12 with 3 possible outcomes:

2a If 1,2,5 vs 3,6,12 balances, then either 4 is the odd light ball or 7 or 8 is the odd heavy ball. Weigh 7 vs 8, if they balance then 4 is the odd light ball, or the heaviest of 7 vs 8 is the odd heavy ball.

2b If 3,6,12 is heavy then either 6 is the odd heavy ball or 1 or 2 is the odd light ball. Weigh 1 vs 2, if balanced then 6 is the odd heavy ball, or the lighest of 1 vs 2 is the odd light ball.

2c If 3,6,12 is light then either 3 is light or 5 is heavy. Weigh 3 against any other ball, if balanced then 5 is the odd heavy ball else 3 is the odd light ball.



3. If 1,2,3,4 > 5,6,7,8 then either 1,2,3,4 contains a heavy ball or 5,6,7,8 contains a light ball so weigh 5,6,1 vs 7,2,12 with 3 possible outcomes:

3a If 5,6,1 vs 7,2,12 balances, then either 8 is the odd light ball or 3 or 4 is the odd heavy ball. Weigh 3 vs 4, if they balance then 8 is the odd light ball, or the heaviest of 3 vs 4 is the odd heavy ball.

3b If 7,2,12 is heavy then either 2 is the odd heavy ball or 5 or 6 is the odd light ball. Weigh 5 vs 6, if balanced then 2 is the odd heavy ball, or the lighest of 5 vs 6 is the odd light ball.

3c If 7,2,12 is light then either 7 is light or 1 is heavy. Weigh 7 against any other ball, if balanced then 1 is the odd heavy ball else 7 is the odd light ball.

For more such articles, Visit: http://www.pankajbatra.com

100 doors in a row

You have 100 doors in a row that are all initially closed. you make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if its open, you close it). the second time you only visit every 2nd door (door #2, #4, #6). the third time, every 3rd door (door #3, #6, #9), etc, until you only visit the 100th door.

for example, after the first pass every door is open. on the second pass you only visit the even doors (2,4,6,8...) so now the even doors are closed and the odd ones are opened. the third time through you will close door 3 (opened from the first pass), open door 6 (closed from the second pass), etc..

question: what state are the doors in after the last pass? which are open which are closed?





Solution: you can figure out that for any given door, say door #42, you will visit it for every divisor it has. so 42 has 1 & 42, 2 & 21, 3 & 14, 6 & 7. so on pass 1 i will open the door, pass 2 i will close it, pass 3 open, pass 6 close, pass 7 open, pass 14 close, pass 21 open, pass 42 close. for every pair of divisors the door will just end up back in its initial state. so you might think that every door will end up closed? well what about door #9. 9 has the divisors 1 & 9, 3 & 3. but 3 is repeated because 9 is a perfect square, so you will only visit door #9, on pass 1, 3, and 9... leaving it open at the end. only perfect square doors will be open at the end.

For more such articles, Visit: http://www.pankajbatra.com

Tuesday, April 24, 2007

Bulbs and Switches

There are three bulbs on 19th floor and there are three switches X, Y and Z on the ground floor. Each switch belongs to one bulbs, not necessarily in order. You can switch on or off as many times you want but you can go on 19th floor only once.

How will you find out which swich belongs to which bulb? Note that you are the only person over there. You can't go outside and can't use any tools.








Answer :
switch X on and leave for 5 minutes. The turn that on off. switch Y on then go up to 19th floor. X switches the lightbulb that is warm but off, Y the one that is lit and Z the cold lightbulb that is off. Pretty simple huh....

For more such articles, Visit: http://www.pankajbatra.com

Elevator

Question: A man lives on the twelfth floor of an apartment building. Every morning he takes the elevator down to the lobby and leaves the building. In the evening, he gets into the elevator, and, if there is someone else in the elevator -- or if it was raining that day -- he goes back to his floor directly. Otherwise, he goes to the tenth floor and walks up two flights of stairs to his apartment.

Answer: The man is a dwarf. He can't reach the upper elevator buttons, but he can ask people to push them for him. He can also push them with his umbrella.

For more such articles, Visit: http://www.pankajbatra.com

Cricketers and Bridge

Seven cricketers must cross a bridge. It is night and they have only one flashlight to guide them on the bridge. A maximum of three people can cross the bridge at one time. Each cricketer walks at a different speed.

Sachin Tendulkar takes 1 minute to cross; Rahul Dravid takes 2 minutes, Brian Lara takes 6 minutes, Ricky Ponting takes 7 minutes, Chaminda Vass takes 8 minutes, Andrew Flintoff takes 9 minutes and Inzamam takes 10 minutes.

They must walk together at the rate of the slower man’s pace i.e. if Sachin Tendulkar, Ricky Ponting and Inzamam cross the bridge together, they will take 10 minutes.

How many minutes will they take to cross the bridge? Give the minimum possible answer.

Answer

They will take 25 minutes to cross the bridge.

Sachin Tendulkar and Rahul Dravid go across – 2 minutes
Sachin Tendulkar comes back – 1 minute
Chaminda Vass, Andre Flintoff and Inzamam go across – 10 minutes
Rahul Dravid comes back – 2 minutes
Sachin Tendulkar, Brian Lara and Ricky Ponting go across – 7 minutes
Sachin Tendulkar comes back – 1 minute
Sachin Tendulkar and Rahul Dravid go across – 2 minutes

Thus, they will take 25 minutes to cross the bridge.

For more such articles, Visit: http://www.pankajbatra.com

Billiard Balls

Suppose you had eight apparently identical billiard balls, but one of the eight is slightly heavier than the others. The only tool you have to measure is a balance scale. What's the fewest number of times you'd have to use the scale to find the heavier ball?

Answer: 2

You can determine exactly which ball is heavier by using the scale only twice. The key to the puzzle is not only getting information by what you measure but also gaining information but what you do not measure.

Here's how:

Separate the 8 balls into 3 piles: two piles of 3 balls and one pile of 2 balls.

Use the scale to compare the two piles of 3 balls. If the heavier ball is in one of the piles of three, then you can use the scale to compare two of the three. You will either have the heavier ball or the ball not measured is the heavier.

If the ball isn't in one of the piles of 3 weighed, then it's in the pile of two and you can use the scale to compare those.


For more such articles, Visit: http://www.pankajbatra.com

Arrays of numbers

We have two arrays of numbers (say a and b).
One is filled (say a) and the other (b) is empty. Now we have to fill the empty array (b) in such a way that number on any position is multiplication of all numbers except the number on same position in the other array.

arrayB[0]=arrayA[1]*arrayA[2]*arrayA[3]*arrayA[4].......*arrayA[arrayA.length-1]
arrayB[1]=arrayA[0]*arrayA[2]*arrayA[3]*arrayA[4].......*arrayA[arrayA.length-1]
arrayB[2]=arrayA[0]*arrayA[1]*arrayA[3]*arrayA[4].......*arrayA[arrayA.length-1]
...............
arrayB[arrayA.length-1]=arrayA[0]*arrayA[1]*arrayA[2]*arrayA[3].....*arrayA[arrayA.length-2]

Give best method to do this.

One way is to find total multiplication of all numbers in arrayA and then find the number for arrayB by dividing the total amount by number on same position is arrayA.
i.e.
totalAmount=1;
for(int i=0; i<arrayA.length; i++)
{
totalAmount=totalAmount*arrayA[i];
}
for(int i=0; i<arrayA.length; i++)
{
arrayB[i]=totalAmount/arrayA[i];
}

Other way of doing so, is the following:

int totalBefore=1;
int totalAfter=1;
for(int i=0; i< arrayA.length; i++)
{
arrayB[i]=1;
}

for(int i=1;i< arrayA.length; i++)
{
totalBefore = totalBefore * arrayA[i-1];
arrayB[i] *= totalBefore;
}

for(int i=arrayA.length-2;i >= 0;i--)
{
totalAfter = totalAfter * arrayA[i+1];
arrayB[i] *= totalAfter;
}
For more such articles, Visit: http://www.pankajbatra.com

Monday, April 23, 2007

Tennis tournament

Question: There's a tennis tournament with one hundred twenty seven players. You've got one hundred twenty-six people paired off in sixty-three matches, plus one unpaired player as a bye. In the next round, there are sixty-four players and thirty-two matches. How many matches, total, does it take to determine a winner?
Answer: One hundred twenty-six. It takes one match to eliminate one player. One hundred twenty-six players have to be eliminated to leave one winner. Therefore, there have to be 126
matches.

For more such articles, Visit: http://www.pankajbatra.com

Jars and Pills

Question: You have 5 jars of pills. Each pill weighs 10 gram, except for contaminated pills contained in one jar, where each pill weighs 9 gm. Given a scale, how could you tell which jar had the contaminated pills in just one measurement?
Answer: 1. Mark the jars with numbers 1, 2, 3, 4, and 5. 2. Take 1 pill from jar 1, take 2 pills from jar 2, take 3 pills from jar 3, take 4 pills from jar 4 and take 5 pills from jar 5. 3. Put all of them on the scale at once and take the measurement. 4. Now, subtract the measurement from 150 ( 1*10 + 2*10 + 3*10 + 4*10 + 5*10) 5. The result will give you the jar number which has contaminated pill.

For more such articles, Visit: http://www.pankajbatra.com

Boxes and Balls

Question : There are 3 boxes. One of them has red balls, one has blue balls only and the other has mixture of red and blue balls. The labels on their boxes always lie. (i.e. if the label says red, you are sure that it doesn't have red balls only,it could be a mixture) The task is to pick one box and pick only one ball from it and then correctly label all the three boxes.
Hint: There are only two combinations of distributions in which ALL the boxes have wrong labels. By picking a ball from the one labeled MIXTURE, it is possible to tell what the other two boxes have.

For more such articles, Visit: http://www.pankajbatra.com