Comparison without relational operators

Source: Quant interview at Religare Technova

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

Comments

  1. int d = a - b;
    if(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 :)
    }

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

      Delete
  2. say 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)

    Reply Delete
  3. Will this work?

    int compare(int64_t a, int64_t b)
    {
    return ((uint64_t) (a - b)) >> 63;
    }

    Reply Delete
  4. Sorry but i couldn't make it simpler..

    #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);
    }

    Reply Delete
  5. int main () {

    int 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");
    }

    Reply Delete
  6. int ret_max(int a, int b){
    int 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;
    }

    Reply Delete
  7. //to check if no's are equal
    if(!(a^b))
    print(equal)
    else if (a/b)
    print(b greater than a)
    else
    print(a greater than b)

    Reply Delete
    Replies
    1. //this should be vice versa
      else 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.

      Delete

Post a Comment

[フレーム]

Popular posts from this blog

Buying Dimsums

Source: Alok Goyal (Stellaris VP, Ex-Helion VC) puzzle blog Problem: A fast food restaurant sells dimsums in boxes of 7 and 3. What’s the greatest number of dimsums a person cannot buy. Generalize it for p and q where p and q are relatively prime. I loved the puzzle. Hope you enjoy it too.

Polya's Urn Problem

Puzzle: There are two urns with one ball each. Each of subsequent n-2 balls is placed into one of these urns, with probability proportional to the number of balls already in that urn. What is the expected number of balls in the smaller sized urn? Source: P. Winkler's Puzzles book. (Chapter: Probability). Solution: Highlight the part between the * symbols for the answer. * This problem can be reformulated as the following problem. Suppose I have a stack of black cards and one red card. Initially I take red card in my hand. Now I add black cards randomly between any two cards (so, initially its either above or below red). Note that the probability that I add the card above the red card, when x-1 is the number of cards above red and y-1 is the number of cards below red is x/(x+y). Let the problem be if red card is dividing the black cards into two sets, what is the expected number of black cards in the smaller section. So, we see that the two problems are equivalent. No...

Fraction Brainteaser

Source: Sent to me by Gaurav Sinha Problem: Siddhant writes a Maths test and correctly answers 5 out of 6 Arithmetic questions and 20 out of 28 Geometry questions.  In total, Siddhant scores 25 out of 34.  Vaibhav writes another Maths test and correctly answers 20 out of 25 Arithmetic questions and 6 out of 9 Geometry questions. in total, Vaibhav scores 26 out of 34. Note that a) Vaibhav scores more than Siddhant b) Siddhant score better than Vaibhav in both individual topics -  5/6 > 20/25 and 20/28 > 6/9 How is it possible?