Velocity Reviews > greatest of two numbers

# greatest of two numbers

aarklon@gmail.com
Guest
Posts: n/a

 12-07-2007
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 ....?????

Richard Heathfield
Guest
Posts: n/a

 12-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
question."

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

jaysome
Guest
Posts: n/a

 12-07-2007
On Fri, 07 Dec 2007 07:46:04 +0000, Richard Heathfield
<(E-Mail Removed)> wrote:

>(E-Mail Removed) 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
>question."

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

You're hired.

Best regards
--
jay

Klutz_wd
Guest
Posts: n/a

 12-07-2007

> 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
> question."
>

funny!

Marco Manfredini
Guest
Posts: n/a

 12-07-2007
(E-Mail Removed) 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.

Marco Manfredini
Guest
Posts: n/a

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

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

SRY

Thomas X. Iverson
Guest
Posts: n/a

 12-07-2007
On 12月7日, 下午3时46分, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
> question."
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> "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

James Kuyper
Guest
Posts: n/a

 12-07-2007
(E-Mail Removed) 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.

stdazi@gmail.com
Guest
Posts: n/a

 12-07-2007
On Dec 7, 6:33 am, (E-Mail Removed) 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;}

Neil Cerutti
Guest
Posts: n/a

 12-07-2007
On 2007-12-07, (E-Mail Removed) <(E-Mail Removed)> 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