### Comparison without relational operators

**Source:**Quant interview at Religare Technova

**Problem:**Write a C program to compare two integers without using relational operators (== != < <= > >=)

This is not a puzzle. So, for those of you who follow this puzzle blog, please bear with me for just one post. Interesting Math in this article though :P Most of my friends already read an article that I wrote more than an year back - " Speak Up " Here, inspired by the movie, The Beautiful Mind, I give a mathematical analysis of asking a girl out. Nice time it is. Feb 10. No plans for Feb 14 and I am sure this article makes me look even more geekier and all the more reason for me to believe that I will be alone, yet again. But what the hell, lets do it! Note: This is not an independent analysis. There are many "mathematics sites" which does "similar" analysis. @Consultants, correct me if I am wrong in my estimates. :P Why is there a need to be selective? From the age of 15, I guess there are approximately 3,600 girls I have liked (On average days, I don't see new girls. But going outside, I like about 30 girls. Saying that I go out once

Once again.. Another coin puzzle after this and this (I wonder whether puzzles have anything else other than hats, kings and coins :D) I heard this from Rushabh Sheth (Mech Sophie) who got it from Vivek Jha (Elec Thirdie). There are 100 coins on the table out of which 50 are tail-face up and 50 are head face up. You are blind folded and there is no way to determine which side is up by rubbing, etc. You have to divide the 100 coins in two equal halves such that both have equal number of coins with tails face up. (This obviously implies that the two have equal number of coins with heads face up) Second part: There are 100 coins on the table out of which 10 are tail-face up and 90 are head face up. You are blind folded and there is no way to determine which side is up by rubbing, etc. You have to divide the 100 coins in two halves (not necessarily equal) such that both have equal number of coins with tails face up. Update (02/01/10): Sorry for inducing confusion in the system. :

Let's say A keep tossing a fair coin, until he get 2 consecutive heads, define X to be the number of tosses for this process; B keep tossing another fair coin, until he get 3 consecutive heads, define Y to be the number of the tosses for this process. 1) Calculate P{X>Y} 2) What's the expected value of X 3) What's the expected value of Y This is probably the hardest puzzle I have ever put on my blog. Hence, I will post its solution in the post directly rather than on comment. Solution: 1) (Solved by me finally after 13 months :)) Make a state diagram. Let the state be (a,b) where a is the number of consecutive heads streak "A" is on currently and b is the number of consecutive heads streak "B" is on currently. So, (0,3) (1,3) are final accepted states and (2,0) (2,1) (2,2) (2,3) are failure states. If you get tails, your contribution to the state reaches to "0" f(State) = P(X>Y | "State" configuration initially)

int d = a - b;

ReplyDeleteif(d)

{

// a is not equal to b

if( d - abs(d) )

{

// d was negative => b > a

}

else

{

// d was positive => a > b

}

}

else

{

// a == b :)

}

In the implementation of abs(), a relational operator is used to determine whether the number passed is less than or greater than zero.

Deletesay 2 nos are a and b in their n-bit representations. subtract b from a by taking 2's complement of b and adding it to a, multiply it by 100...(n-1 0s). then right-shift the no n-1 times. call it c. multiply c by b. then complement c, multiply it by a. now max = b*c+a*(complement of c)

ReplyDeleteWill this work?

ReplyDeleteint compare(int64_t a, int64_t b)

{

return ((uint64_t) (a - b)) >> 63;

}

Sorry but i couldn't make it simpler..

ReplyDelete#include

#include

int maximum(int x,int y)

{

return(x+y+sqrt(-2*y*x +x*x + y*y))/2;

}

int main()

{

int a,b,c;

scanf("%d%d",&a,&b);

c=maximum(a,b);

printf("\n%d",c);

}

int main () {

ReplyDeleteint a,b;

printf ( "enter the two values u like to compare\n");

scanf (" %d %d",&a,&b);

if (!(a ^ b))

printf ("both are equal\n);

else

printf ("both are not equal\n");

}

int ret_max(int a, int b){

ReplyDeleteint c=a,d=b;

while (c&d){

c&=(c-1);

c&=(d-1);

}

if (c)

return a;

else // also takes care of equality

return b;

}

//to check if no's are equal

ReplyDeleteif(!(a^b))

print(equal)

else if (a/b)

print(b greater than a)

else

print(a greater than b)

//this should be vice versa

Deleteelse if (a/b)

print(a greater than b)

else

print(b greater than a)

//also this will not work if one number is negative for example a = 1 and b = -5.