edited by
29,950 views
42 votes
42 votes

Consider the following function:

int unknown(int n){ 

int i, j, k=0; 
for (i=n/2; i<=n; i++) 
    for (j=2; j<=n; j=j*2) 
        k = k + n/2; 
 return (k); 

} 

The return value of the function is

  1. $\Theta(n^2)$      
  2. $\Theta(n^2\log n)$      
  3. $\Theta(n^3)$      
  4. $\Theta(n^3\log n)$
edited by

5 Answers

0 votes
0 votes
The i loop will run for $n/2$ times while the j loop will run for $log n$ times .
the value of k is incremented by n/2 everytime so the correct answer will be $(n^{2} log n) /4$ which is assymptotically  ϴ$(n^{2} log n)$
Answer:

Related questions

113 votes
113 votes
9 answers
5
Arjun asked Sep 24, 2014
28,478 views
The number of elements that can be sorted in $\Theta(\log n)$ time using heap sort is$\Theta(1)$$\Theta(\sqrt{\log} n)$$\Theta(\frac{\log n}{\log \log n})$$\Theta(\log n)...
34 votes
34 votes
4 answers
6
Arjun asked Sep 23, 2014
24,411 views
What is the time complexity of Bellman-Ford single-source shortest path algorithm on a complete graph of n vertices?$\theta(n^2)$ $\theta(n^2\log n)$ $\theta(n^3)...