Sunday, April 13, 2025
Google search engine
HomeData Modelling & AIAlgorithms | Analysis of Algorithms (Recurrences) | Question 8

Algorithms | Analysis of Algorithms (Recurrences) | Question 8

What is the time complexity of the following recursive function: 
 

c




int DoSomething (int n)
{
  if (n <= 2)
    return 1;
  else 
    return (DoSomething (floor(sqrt(n))) + n);
}


(A) \\theta   (n) 
(B) \\theta   (nlogn)
(C) \\theta   (logn)
(D) \\theta   (loglogn)
 

(A)

A

(B)

B

(C)

D

(D)

C

Answer: (C)
Explanation:

Recursive relation for the DoSomething() is 
 

  T(n) =  T(\\sqrt{n}) + C1 if n > 2  

We have ignored the floor() part as it doesn\’t matter here if it\’s a floor or ceiling. 
 

  Let n = 2^m,  T(n) = T(2^m)
  Let T(2^m) =  S(m)

  From the above two, T(n) = S(m)

  S(m) = S(m/2) + C1  /* This is simply binary search recursion*/
  S(m)  = O(logm)      
          = O(loglogn)  /* Since n = 2^m */
  
  Now, let us go back to the original recursive function T(n) 
  T(n)  = S(m) 
          = O(LogLogn)

 

Quiz of this Question
Please comment below if you find anything wrong in the above post

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments