• Status: Solved
• Priority: Medium
• Security: Public
• Views: 317

# [Algorithms] Finding numbers

Suppose x, y (distinct) belongs to a set of integers from 0 to n-1.

Consider a  game bwetween players A and B. PLayer A keeps the 2 numbers x, y. B wants to find out x, y by asking questions.

a) Suppose A will reply either Yes or No. At least how many quetions B must ask to determine x, y?
My ans which i think is not correct: log (n) + log (n-1)
Any suggestions?

b) Suppose B can ask A in the following way:
B gives A  a number r.
A tells B whether x > r, and whether y > r.

Hence for one question asked by B, there are 4 possible outcomes from A.
What's a good algorithm for B?

???
0
acrilas
• 2
3 Solutions

Commented:
We really shouldn't do your homework for you, but you have at least made an effort.
Suppose you guess a number r, 0 <= r < n.

In case a) you have a 1/n chance of guessing the correct number, but if you are wrong
answers is reduced by just 1.   On average, it would take you n/2 + n/2 guesses to
determine x and y.  Worst case would be 2n guesses (suppose x and y were both n-1).

In case b) if you guess wrong, you can subset the numbers.  Suppose your initial guess
is r = n/2, and it is wrong, say A says x < r.  In that case you can eliminate half of the
possible answers - limiting the next guess between 0 and r-1 (do you sense recursion here?).
This is called a binary search (dividing the solution space in half each time).  I leave it up
to you to determine the maximum number of comparisons needed to determine both
values in this case.

0

Commented:
> so the number of valid answers is reduced by just 1.

so the number of *possible* valid answers is reduced by just 1.

0

Commented:
Can B ask A any type of Yes/No question?

If so A simple binary search will find each number in the fewest questions.
Question 1 - Is X less than (n-1) / 2
If Yes
Question 2 - Is X less than (n-1) / 4
If No
Question 2 - Is X less than (n-1) - (n-1) / 4
Repeat for Y.

example
0 - 128
Is X < 64?
Yes
Is X < 32?
or
No
Is X less than 96?

B. Will work similarly once x < guess and y > guess

For part A - Actually the way you have written the question, the answer is 1 question must be asked.  You may need more but only one question must be asked.
Is x= Guess1 and y = Guess2?  If correct you are done.

mlmcc

0

Commented:
Don't know why my comment wasn't posted.

For question B, the answer is less trivial.
Here is my reasoning.
You may consider that you search x and in the same time get some information on the value on y.
So in any case you need log(n) questions to locate x.

Now what to do with the information you get on y. You have to consider two possible answer: get same answer for x and y or get a different answer for x and y. If you get a different answer this means that r is between x and y.
When you start with r = n/2 as you would do to search x, you will also know if y is on same side of r or on the opposite side. In any case you will have reduced the search space for y by two. But if y is on the opposite side of r, any subsequent question to locate x won't give you usefull information to locate y. In this case you will need an additional log(n/2) questions to locate y. There are 50% chances that this case happen. Or on the average 50% searches will cost you log(n)+log(n/2) questions.

Now if you learn after the first question that x and y are on the same side. Then you save a question for y. Ask then the next question for x. If we get then a different answer for y, we then need log(n/4) additional questions to locate y. This will happen 25% of the time (=0.5*0.5).

Thus the number of questions can be easily generalised into log(n) + 0.5*log(n/2) + 0.25*log(n/4) + 0.125*log(n/8) + ...

This can be generalised into

sum for i = 0 to log(n) of log( n/(2^i))/(2^i)

The term can be reorganised into [ log(n) - log(2^i) ] / (2^i) = [log(n)-i]/2^i
You can split this into two sums so that you have

log(n)*sum(1/2^i) - sum(i/2^i)
The first sum is equal to 1-1/(2^log(n)) which is also equal to 1-1/n

So the number of questions to ask is log(n)(1-1/n) - sum(i/2^i)
I could not find how to simplify the second term.

Hope this helps and that my comment gets published !

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.