greatest of two numbers - C

This is a discussion on greatest of two numbers - C ; Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) ) /2 ; is there any better ...

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 38

greatest of two numbers

  1. Default greatest of two numbers

    Hi all,

    The following question is asked frequently in interviews

    How to find the greatest of 2 numbers without using relational
    operators ?

    the solution i have seen is

    ( a+b + abs(a-b) ) /2 ;

    is there any better solution than this ....?????

  2. Default Re: greatest of two numbers

    aarklon@gmail.com said:

    > Hi all,
    >
    > The following question is asked frequently in interviews
    >
    > How to find the greatest of 2 numbers without using relational
    > operators ?


    The proper answer is: badly. The relational operators are there for a
    reason.

    > the solution i have seen is
    >
    > ( a+b + abs(a-b) ) /2 ;
    >
    > is there any better solution than this ....?????


    Yes - use the relational operators! It's a pretty good bet that abs() uses
    one anyway, and one major advantage of using a relational operator rather
    than the code you show is that you avoid the two additions, the
    subtraction, possibly a function call, and a division. You also avoid no
    fewer than three overflow hazards.

    So my answer to the interviewer would be: "sir (or madam), I guess that you
    are looking for a trick answer, but I don't have one. All I have is the
    correct answer, which is - use a relational operator. That is the proper
    engineering solution. I can fully accept that there might be a curious and
    interesting trick for doing this, but - whatever it is - it will not be as
    sound a solution as using a relational operator. If you want to hire
    someone who can play programmatic tricks, you don't want me; please look
    elsewhere, because I am a programmer, not a stage magician, and I am
    looking for a client who understands this. But if you are after someone
    who can point at the Emperor and say 'why doesn't that silly man go and
    get dressed?', someone who can write clear, concise, obvious code, and use
    the right operators for the right jobs, then please ask your next
    question."

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

  3. Default Re: greatest of two numbers

    On Fri, 07 Dec 2007 07:46:04 +0000, Richard Heathfield
    <rjh@see.sig.invalid> wrote:

    >aarklon@gmail.com said:
    >
    >> Hi all,
    >>
    >> The following question is asked frequently in interviews
    >>
    >> How to find the greatest of 2 numbers without using relational
    >> operators ?

    >
    >The proper answer is: badly. The relational operators are there for a
    >reason.
    >
    >> the solution i have seen is
    >>
    >> ( a+b + abs(a-b) ) /2 ;
    >>
    >> is there any better solution than this ....?????

    >
    >Yes - use the relational operators! It's a pretty good bet that abs() uses
    >one anyway, and one major advantage of using a relational operator rather
    >than the code you show is that you avoid the two additions, the
    >subtraction, possibly a function call, and a division. You also avoid no
    >fewer than three overflow hazards.


    Agree.

    >So my answer to the interviewer would be: "sir (or madam), I guess that you
    >are looking for a trick answer, but I don't have one. All I have is the
    >correct answer, which is - use a relational operator. That is the proper
    >engineering solution. I can fully accept that there might be a curious and
    >interesting trick for doing this, but - whatever it is - it will not be as
    >sound a solution as using a relational operator. If you want to hire
    >someone who can play programmatic tricks, you don't want me; please look
    >elsewhere, because I am a programmer, not a stage magician, and I am
    >looking for a client who understands this. But if you are after someone
    >who can point at the Emperor and say 'why doesn't that silly man go and
    >get dressed?', someone who can write clear, concise, obvious code, and use
    >the right operators for the right jobs, then please ask your next
    >question."


    Or perhaps that was the "trick" answer the interviewer was looking
    for.

    You're hired.

    Best regards
    --
    jay

  4. Default Re: greatest of two numbers


    > So my answer to the interviewer would be: "sir (or madam), I guess that you
    > are looking for a trick answer, but I don't have one. All I have is the
    > correct answer, which is - use a relational operator. That is the proper
    > engineering solution. I can fully accept that there might be a curious and
    > interesting trick for doing this, but - whatever it is - it will not be as
    > sound a solution as using a relational operator. If you want to hire
    > someone who can play programmatic tricks, you don't want me; please look
    > elsewhere, because I am a programmer, not a stage magician, and I am
    > looking for a client who understands this. But if you are after someone
    > who can point at the Emperor and say 'why doesn't that silly man go and
    > get dressed?', someone who can write clear, concise, obvious code, and use
    > the right operators for the right jobs, then please ask your next
    > question."
    >


    funny!

  5. Default Re: greatest of two numbers

    aarklon@gmail.com wrote:
    > Hi all,
    >
    > The following question is asked frequently in interviews
    >
    > How to find the greatest of 2 numbers without using relational
    > operators ?
    >
    > the solution i have seen is
    >
    > ( a+b + abs(a-b) ) /2 ;
    >
    > is there any better solution than this ....?????


    Sure:

    int max(int x, int y)
    {
    int d=x-y;
    return y + d & ((~(d^((x^y)&(d^x))))>>31);
    }

    Watch out for over/underflows though.

  6. Default Re: greatest of two numbers

    Marco Manfredini wrote:
    > return y + d & ((~(d^((x^y)&(d^x))))>>31);

    return y + (d & ((~(d^((x^y)&(d^x))))>>31));

    SRY


  7. Default Re: greatest of two numbers

    On 12月7日, 下午3时46分, Richard Heathfield <r...@see.sig.invalid> wrote:
    > aark...@gmail.com said:
    >
    > > Hi all,

    >
    > > The following question is asked frequently in interviews

    >
    > > How to find the greatest of 2 numbers without using relational
    > > operators ?

    >
    > The proper answer is: badly. The relational operators are there for a
    > reason.
    >
    > > the solution i have seen is

    >
    > > ( a+b + abs(a-b) ) /2 ;

    >
    > > is there any better solution than this ....?????

    >
    > Yes - use the relational operators! It's a pretty good bet that abs() uses
    > one anyway, and one major advantage of using a relational operator rather
    > than the code you show is that you avoid the two additions, the
    > subtraction, possibly a function call, and a division. You also avoid no
    > fewer than three overflow hazards.
    >
    > So my answer to the interviewer would be: "sir (or madam), I guess that you
    > are looking for a trick answer, but I don't have one. All I have is the
    > correct answer, which is - use a relational operator. That is the proper
    > engineering solution. I can fully accept that there might be a curious and
    > interesting trick for doing this, but - whatever it is - it will not be as
    > sound a solution as using a relational operator. If you want to hire
    > someone who can play programmatic tricks, you don't want me; please look
    > elsewhere, because I am a programmer, not a stage magician, and I am
    > looking for a client who understands this. But if you are after someone
    > who can point at the Emperor and say 'why doesn't that silly man go and
    > get dressed?', someone who can write clear, concise, obvious code, and use
    > the right operators for the right jobs, then please ask your next
    > question."
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    you are right , to write a program simply and stupid is the best way
    for developing and maintaining , algorithm is not the most important
    thing in developing
    Keep It Simple , Stupid!!!

    the author's answer is good enough , i have no idea about the better
    solution

  8. Default Re: greatest of two numbers

    aarklon@gmail.com wrote:
    > Hi all,
    >
    > The following question is asked frequently in interviews
    >
    > How to find the greatest of 2 numbers without using relational
    > operators ?
    >
    > the solution i have seen is
    >
    > ( a+b + abs(a-b) ) /2 ;
    >
    > is there any better solution than this ....?????


    As people have pointed out, this is a stupid question. The solution you
    have works fine as long as it doesn't overflow. Still, it would be nice
    to avoid the overflow. If these were floating point numbers, all you
    would have to do is divide by 2 before overflow can happen:

    ( a/2 + b/2 + abs(a/2-b/2))

    (There's no free lunch: with the corresponding expression for floating
    point numbers you'd have to worry about loss of precision due to underflow)

    Unfortunately, since this is integer arithmetic, the result is only an
    approximation to the correct number. It is inaccurate due to the fact
    that 2*(n/2) != n if n is odd. This can be fixed up by using a%2 and b%2
    in some clever fashion that I'm leaving as an exercise for the reader,
    because this IS a stupid question and I don't want to bother figuring it
    out for myself.

  9. Default Re: greatest of two numbers

    On Dec 7, 6:33 am, aark...@gmail.com wrote:
    > Hi all,
    >
    > The following question is asked frequently in interviews
    >
    > How to find the greatest of 2 numbers without using relational
    > operators ?
    >
    > the solution i have seen is
    >
    > ( a+b + abs(a-b) ) /2 ;
    >
    > is there any better solution than this ....?????


    Stupid question, anyway :

    #include <math.h>
    int maximum(int x,int y){return(x+y+sqrt(-2*y*x +x*x + y*y))/2;}

  10. Default Re: greatest of two numbers

    On 2007-12-07, aarklon@gmail.com <aarklon@gmail.com> wrote:
    > Hi all,
    >
    > The following question is asked frequently in interviews
    >
    > How to find the greatest of 2 numbers without using relational
    > operators ?


    If the hopeful applicant gets this one right, the next question
    is usually: How to make me a cambric shirt without no seem nor
    needlework?

    --
    Neil Cerutti

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast

Similar Threads

  1. How to find the greatest of two numbers without using the comparison operators?
    By Application Development in forum Programming Languages
    Replies: 183
    Last Post: 09-20-2010, 06:19 AM
  2. Re: greatest and least of these...
    By Application Development in forum Python
    Replies: 0
    Last Post: 10-23-2007, 12:10 PM
  3. Printing columns of numbers and align numbers
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 09-28-2007, 01:35 AM
  4. Replies: 2
    Last Post: 07-25-2007, 08:46 PM
  5. Greatest of three numbers
    By Application Development in forum C
    Replies: 30
    Last Post: 07-03-2007, 09:20 PM