edited by
19,528 views
35 votes
35 votes

Consider the following C-program fragment in which $i$, $j$ and $n$ are integer variables. 

 for( i = n, j = 0; i > 0; i /= 2, j +=i );

Let $val(j)$ denote the value stored in the variable $j$ after termination of the for loop. Which one of the following is true? 

  1. $val(j)=\Theta(\log n)$
  2. $val(j)=\Theta (\sqrt{n})$
  3. $val(j)=\Theta( n)$
  4. $val(j)=\Theta (n\log n)$
edited by

10 Answers

0 votes
0 votes

I solved it like this:

i= 32, so j = 16+4+2 = 22

22 is not around log32, also not around 32log32, also not around sqrt(32), so only thing remaining is theta(n), also its theta and not big O.

 

Answer:

Related questions

90 votes
90 votes
12 answers
4