Let x,y,z be 3 integers . We have to find the greatest of 3 integers, without using relational and conditional operator?
ANSWER IS THERE IN COMMENTs ,still u can add ur answer .
Subscribe to:
Post Comments (Atom)
This is the blog of my interest.(Chess,funny Tech,puzzle etc)
10 comments:
i had this question in my google interview
Try this solution
step 1:int a,b,c;
step 2:a=3;b=5;c=7;
step 3:int d=(a+b+2*c+Abs(a-b)+Abs(a+b-2*c+Abs(a-b)))/4;
Yes ur answer is rite ,
for two variable a and b
max=(a+b+abs(a-b))/2
so we can derive three variable or more.
My Answer1: is in the above comment.
ANSWER 2:
int max (int numer1 , int number2)
{
int array[2] = { number1 ,number2 }
return ( array [ !!(number1 / number2 )]
Answer 3:
!!(number1-number2) && return number1
!!(number2-number1) && return number2
Answer 4 :
(number1-number2)& 0x1000000 && return number1
(number2-number1)& 0x1000000 && return number2
note right most bit checking method
The equation below will work only if x != y != z.
MaxOf(x, y ,z) = x*((y - x) & (z - x)& 0x8000)/0x8000
+ y*((x - y) & (z - y)& 0x8000)/0x8000
+ z*((x - z) & (y - z)& 0x8000)/0x8000;
int a,b,c; //a,b,c are the three number initially
int k,m,n;
k=a/b;
m=b/a;
n=(ka+mb)/(k+m); // here we get the biggest of two numbers
now
a=n;
b=c;
n=(ka+mb)/(k+m); //this wil give the biggest of three number a,b,c
Post a Comment