I m getting 66 comparisons but ans given as 65..plz verify @Shaik Masthan @MiNiPanda

0 votes

We are given a C function, mystery() as follows.

void mystery(int m, int n) { while(m<=n) { m++; n--; } }

Let X be the number of times the comparission inside the while loop ( i.e., m<=n ) is performed, when mystery(127,255) is called.

Then the value of X is _______________

2

In each iteration, m will be increased by 1 and n will be decreased by 1 (also 1 comparison made).

So in k iterations, m will become (m+k) and n will become (n-k) (k comparisons made).

The last comparison made will be when (m+k)>(n-k).

m+k>n-k

2k>n-m

k>128/2>64

As k>64 so at 65th iteration (65th comparison) m will become more than n.

After that at 66th iteration the while condition won't be satisfied. So 66 comparisons are needed.

Please check.

0

yes ! it should be 66

when the conflict arises, we will take a small input and analysis !

let for mystery(11,15)

11 is compared with 15

12 is compared with 14

13 is compared with 13

14 is compared with 12 ===> stop, total = 4 comparissions !

when the conflict arises, we will take a small input and analysis !

let for mystery(11,15)

11 is compared with 15

12 is compared with 14

13 is compared with 13

14 is compared with 12 ===> stop, total = 4 comparissions !

0

No but I took the test on the official site. So you can tell me the question no. I guess it will match.