in DS edited by
699 views
0 votes
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 _______________

in DS edited by
by
699 views

10 Comments

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

0
0

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.

@Shaik Masthan 

Please check.

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

@Shaik Masthan 

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

0
0
hoo... i hope you didn't take the test of CN mock-1
0
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
0
UDP socket doesn't require Source IP and Source Port ?
0
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
0
it is from Transport layer !
0
0

1 Answer

0 votes
0 votes
192-127+1= 66 comparisons

Related questions