recategorized by
694 views
2 votes
2 votes

Consider the following program code, where pow() is the exponentiation function.

f (int n)
{
    if (n <= 1) return 1;
    return f(n-1) + g(n) + g(pow(2, n-1));
}

g (int n)
{
    if (n <= 1) return 1;
    return 1 + g(n/2);
}

What is the worst-case time complexity of $f(n)$?

  1. $O(2^n)$
  2. $O(n^2)$
  3. $O(2^n \log (n))$
  4. $O( \log ^n (n))$
recategorized by

2 Answers

2 votes
2 votes
$\begin{array}{ll} T^1(n) & = T^1( \frac{n}{2})+\theta (1) \\ T^1(n) & = O( \log_2 n) \dots (1) \\ T(n) & = T(n-1)+T^1(n) +T^1(2^n) \\ \Leftrightarrow T(n) & = T(n-1)+O( \log_2 n ) +O(n) \text{Using 1} \\ T(n) & = \Sigma_{i=1}^n O(i) + \Sigma_{i=1}^n O( \log i) \\ & = O(n^2)+O( \log n!) \\ & = O(n^2)+O( \log_n n) \\ & = O(n^2)+O( n \log n) \\ & = O(n^2) \end{array}$
So option B is correct.
0 votes
0 votes

@Arjun

Sir can you please verify my answer

from observation we can say time complexity of g(n) is O(log n)

hence time complexity for g($2^{n-1}$) will be O(log($2^{n-1}$)) = O(n)

Now

f(n-1) + g(n) + g(pow(2, n-1));

this statement will be called n times

                        f(n-1)                                                   + g(n)                                                 + g(pow(2, n-1));

        Not doing any work                                                 O(log n)                                               O(n)

hence overall                                                    max(    n times x O(log n)                        ,       n times x O(n))

hecne times complexity is O($n^{2}$)

                            

 

Answer:

Related questions

1 votes
1 votes
1 answer
3
Applied Course asked Jan 16, 2019
525 views
Suppose a radix sort was done on the following set of numbers, in binary i.e,$[11, 10, 3, 14, 12, 2, 8, 15, 2]$. How many passes of counting sort would be performed _____...