Warning: count(): Parameter must be an array or an object that implements Countable in /home/customer/www/thegatebook.in/public_html/qa/qa-include/qa-theme-base.php on line 177

DSA - Grand Test -Q17

Warning: count(): Parameter must be an array or an object that implements Countable in /home/customer/www/thegatebook.in/public_html/qa/qa-include/qa-theme-base.php on line 177

Consider the following program:

#include <stdio.h>

int bar(int n){

  if(n <= 1){

  return 0;

  }

  else{

  return 1 + bar(n / 2);

  }

}

void main(){

  int i, k = 0;

  for(i = 1024; i>=1;i >>=1)

  k = k + bar(i);

  printf("%d",k);

}

The value printed by the program is ______

reshown Sep 10, 2020

+1 vote

The for loop is always dividing i by 2 and

bar function is calculation logn always.

k is storing all the logn for all those steps

k = log(1024) + log(512) + ... + log(2)

= 1 + 2 + 3 +... + 10

$\frac{10(10+1)}{2} = 55$

answered Sep 26, 2020 by (11,240 points)
selected Sep 26, 2020