PA of Algorithms Q50

0. Consider the following segment of C code
int j, n;
j = i;
while (j < = n)
j = j*2;
The number of comparisons made in the execution of the loop for any n > 0 is

  • Option : D
  • Explanation :
    Since, j increases in power of 2's.
    if statement j = j * 2 executes k times, then
    2k < n
    ⇒ k < log2 n
    Since k will be integer,
    total number of comparison
    (when loop exits)
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *