search
Log In
0 votes
184 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 by
184 views
0

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

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.

@Shaik Masthan 

Please check.

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

@Shaik Masthan 

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 !

1 Answer

0 votes
192-127+1= 66 comparisons

Related questions

3 votes
1 answer
1
526 views
Consider a new sorting algorithm similar to the BubbleSort algorithm, called RumbleSort. Given an array as input, RumbleSort attempts to sort the array and produces a sorted array as output. Here's the pseudo-code for RumbleSort. With regards to the above RumbleSort algorithm, ... algorithm will work correctly for a given input is $\mathcal Ο(n^2)$ Which of the above statements is/are true?
asked Jan 30, 2019 in Algorithms balchandar reddy san 526 views
2 votes
3 answers
2
1k views
Consider n elements that are equally distributed in k stacks. In each stack, elements of it are arranged in ascending order (min is at the top in each of the stack and then increasing downwards). Given a queue of size n in which we have to put all n elements in increasing order. What will be the time complexity of the best known algorithm?
asked Nov 4, 2017 in DS chandra sai 1k views
1 vote
1 answer
3
210 views
Q:in the infix to postfix conversion of this, which operator is computed first? a+b-c*d/e a) + b)* c)- d)/ ANS: according to precedence and assosiativity * shouyld be computed first of all in the expression but answer is given as + are they trying to say it because the postfix ab+cd*e/- is the postfix!!!! but the word 'computed' is confusing
asked Jan 22, 2016 in DS Aboveallplayer 210 views
...