Wednesday, April 11, 2007

a puzzle that haunted me!!!

here is a puzzle that haunted me for a while today... finally, i solved it after struggling for a couple of hours... try this out!!! if you get the solution, you are intelligent... if you don't get it and seriously interested in knowing the solution, mail me for the same... otherwise, you can wait for few days so that i will post the solution too!!!

here it goes: there are twelve balls looking identical (shape, size and color)... but, only one of them is weighing differently (it is not given whether lighter or heavier than the rest)... the puzzle is to find that odd one using a simple balance (without any weights) for only three times!!! also, you should be able to tell whether the odd one is lighter or heavier than the rest!!!

just for fun, i am giving you a c-program below to check whether your logic is correct or not!!! make sure that you change those square brackets to include header files to angular brackets... try it... all the best!

the program

#include[stdlib.h]
#include[math.h]
#include[ctype.h]
#include[stdio.h]
void balance(int, int []);
main()
{
int V[13];
int n,i,k;
int choice,correct,weight,weighed;
float dummy;

for(i=1;i<=12;i++) V[i]=3;
srand(time(NULL));
dummy=rand();
correct=(int)((dummy/RAND_MAX)*12)+1;
dummy=(float)rand()/RAND_MAX;
if(dummy<=0.5) { V[correct]=V[correct]-2; weight=0;}
else { V[correct]=V[correct]+2; weight=1;}
printf("program to find odd man out from a group of 12\n");
printf("you are allowed to use the balance for 3 times\n");
for(k=1;k<=3;k++) { printf("\nyour chance number %d\n",k);
err1: printf("enter the number of objects in each plate:");
scanf("%d",&n);
if(n>6) {printf("\ninvalid choice\n");goto err1;}

else
balance(n,V);
}
printf("\n\nenter the index of odd man:");
scanf("%d",&choice);
if(choice==correct)
{
printf("\nis it heavier or lighter?");
err2: printf("\n1:heavier\t\t0:lighter");
scanf("%d",&weighed);
if((weighed!=0)&&(weighed!=1))
{ printf("\ninvalid choice!"); goto err2;}
if(weighed==weight)
printf("\n\ncongrats! your logic is correct");
else printf("\n\noops! you got it wrong!");
}
else printf("\n\noops! you got it wrong!");
}

void balance(int n,int V[])
{
int i,sumL,sumR,L[6],R[6];
sumL=sumR=0;
for(i=1;i<=n;i++)
{
printf("\nenter L[%d]:",i);
scanf("%d",&L[i]); sumL=sumL+V[L[i]];
}
for(i=1;i<=n;i++)
{
printf("\nenter R[%d]:",i);
scanf("%d",&R[i]); sumR=sumR+V[R[i]];
}
if(sumL==sumR) printf("both sides equal\n");
else if(sumL > sumR) printf("left pan heavier\n");
else printf("right pan heavier");
}

2 comments:

Uday said...

naa valla kavatam ledu boss. phir try karoonga
:-)

... said...

For those who haven't solved it, a hint: you can mark the balls :)