96 views

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 _______________

in DS
edited | 96 views
0

I m getting 66 comparisons but ans given as 65..plz verify

+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.

+1
Answer should be 66, given wrong in the key..
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 !
0

The link where you tagged me few minutes back isn't accessible to me.

0
hoo... i hope you didn't take the test of CN mock-1
0
No but I took the test on the official site. So you can tell me the question no. I guess it will match.
0
UDP socket doesn't require Source IP and Source Port ?
0
Is this from Grand test?

In the official site I took cn 1 and 2 on flow and error control and network layer..

This question seems like it's from transport layer or Grand test..I didn't take any of the 2 :(
0
it is from Transport layer !

192-127+1= 66 comparisons
by (207 points)