• edited by
57,070 views
88 88 votes

Consider the following segment of C-code:

int j, n;
j = 1;
while (j <= n)
    j = j * 2;

The number of comparisons made in the execution of the loop for any $n > 0$ is:

  1. $\lceil \log_2n \rceil +1$

  2. $n$

  3. $\lceil \log_2n \rceil$

  4. $\lfloor \log_2n \rfloor +1$

16 Answers

Best answer
109 109 votes
$$\begin{array}{|c|l|c|} \hline \text{n} & \text {No. of comparisons} & \text{$\lfloor \log_2 n\rfloor + 1$}\\\hline \text{1} &  \text{$2 \;(j = 1,2)$} & \text{1}\\\hline \text{2} & \text{$3\; (j = 1,2,4)$} & \text{2} \\\hline \text{3} & \text{$3\; (j = 1,2,4)$} & \text{2} \\\hline \text{4} & \text{$4 \;(j = 1,2,4,8)$} & \text{3} \\\hline  \text{5} & \text{$4\; (j = 1,2,4,8)$} & \text{3} \\\hline \end{array}$$
We have to count those comparisons which happens during the execution of the loop and so the exit comparison must also be a part. So, the correct answer should be $\lfloor \log_2 n \rfloor + 2.$

Since this is not in the choices we can assume that the question setter excluded the exit comparison and so the answer should be  $\lfloor \log_2 n \rfloor + 1.$

Option D.
• selected by
22 22 votes
Assuming that "comparisons made in the execution of the loop" means that comparison made while loop is executing, last comparison is not counted.

Say we have j = 7.

Then successful comparisons are 1,2,4. (We get out in next comparison.) So 3 comparisons

Then

A) Incorrect as We get 21 !=3.

B) Incorrect, this is 8.

C) Correct

D) Correct

C & D both gave 3

In C & D for tie breaking, we can take j = 8. No of comparisons (Successful) -> 1,2,4,8.(We get out of loop in 16)

C) 3 != 4. Incorrect

D) 4 Correct

 

Answer -> D
21 21 votes
n Number of Comparisons   
1 2(j=1,2)  
2 3(j=1,2,4)  
3 3(j=1,2,4)  
4 4(j=1,2,4,8)  
5 4(j=1,2,4,8)  

Answer should be None of these here.I think Answer should be $\lfloor \log_2n \rfloor +2$ .

• edited by
1 flag:
✌ Low quality (arbpass “last comparison wont be considered in the execution of the loop”)
10 10 votes
Answer is a)

say n=4
According to answer d no of comparisons will be 3
but actually there will be 4 comparison
1<=4
2<=4
4<=4
and final comparison will be 8<=4 which will give false ,So total no of comparisons are 4 So d cant be true

Moreover say for 7 node no of comparisons will be
1<=7
2<=7
4<=7
8<=7 i.e also 4
so answer is ceil (logn)+1 i.e option a
9 9 votes

All the options fail for n = 2, 4, 8, 16... 

The correct answer to this is -  ⌊log2n⌋ + 2

6 6 votes

Answer D). Log2n + 1

Answer:
Position:
Show:

Related questions

53 53 votes
9 answers 9 answers
45.7k
45.7k views
Kathleen asked Sep 21, 2014
45,710 views
The message $11001001$ is to be transmitted using the CRC polynomial $x^3 +1$ to protect it from errors. The message that should be transmitted is:$11001001000$$110010010...
69 69 votes
9 answers 9 answers
38.1k
38.1k views
Kathleen asked Sep 21, 2014
38,113 views
The order of a leaf node in a $B^+$ - tree is the maximum number of (value, data record pointer) pairs it can hold. Given that the block size is $1K\;\text{bytes}$, data ...
51 51 votes
4 answers 4 answers
24.5k
24.5k views
Kathleen asked Sep 21, 2014
24,525 views
The following postfix expression with single digit operands is evaluated using a stack:$$8 \ 2 \ 3 \ \;\hat{}\; ∕ \ 2 \ 3 * + 5 \ 1 * -$$Note that $\;\hat{}\;$ is the ...
39 39 votes
5 answers 5 answers
27.7k
27.7k views
Kathleen asked Sep 21, 2014
27,671 views
Consider a disk pack with $16$ surfaces, $128$ tracks per surface and $256$ sectors per track. $512$ bytes of data are stored in a bit serial manner in a sector. The capa...