GATE CS 2007 · Asymptotic Analysis & Recurrences

MCQ · 1 mark · Problem PROG-DIVC-0138

What is the time complexity of the following recursive function?
int DoSomething (int n)
{
    if (n <= 2)
        return 1;
    else
        return (DoSomething (floor (sqrt (n))) + n);
}
  1. A
    \(\Theta (n^{2})\)
  2. B
    \(\Theta (n \log_{2} n)\)
  3. C
    \(\Theta (\log_{2} n)\)
  4. D
    \(\Theta (\log_{2} \log_{2} n)\)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{T(n)=T(\sqrt{n})+c,\qquad T(n)=c\ \text{ for } n\le 2}\]Each call does constant work and makes one call on \(\lfloor\sqrt n\rfloor\). After \(k\) calls the argument is about \(n^{1/2^k}\), and the recursion stops once \(n^{1/2^k}\le 2\), that is \(2^k\ge\log_2 n\), so \(k=\log_2\log_2 n\).(With \(n=2^m\) and \(S(m)=T(2^m)\): \(S(m)=S(m/2)+c=\Theta(\log m)\).)\(T(n)=\Theta(\log_2\log_2 n)\). The value returned grows like \(n\), but the question asks for the running time.Answer: D

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com