GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Count repeated square-root recursion depth

3 questions solved the same way, newest first. Back to Algorithms

How these are solved

A single recursive subproblem replaces n by a fixed root and adds constant call work. Take logarithms and count repeated division until the base threshold.

1 mark · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 2, GATE 2020: For parameters a,b, both printed as ω(1), T(n)=T(n^(1/a))+1 and T(b)=1. Choose the printed asymptotic expression. Options: A Θ(log_a(log_b n)); B Θ(log_(ab) n); C Θ(log_b(log_a n)); D Θ(log_2(log_2 n)). No cutoff for inputs below b is specified; retain this qualification.
Question 2, GATE 2020: For parameters a,b, both printed as ω(1), T(n)=T(n^(1/a))+1 and T(b)=1. Choose the printed asymptotic expression. Options: A Θ(log_a(log_b n)); B Θ(log_(ab) n); C Θ(log_b(log_a n)); D Θ(log_2(log_2 n)). No cutoff for inputs below b is specified; retain this qualification.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A under the intended recurrence-depth convention

Worked explanation

Unroll without changing the logarithm bases: T(n)=T(n1/a²)+2=…=T(n1/a^j)+j. Exact termination at b requires n1/a^j=b, or (ln n)/aj=ln b. Thus aj=ln n/ln b=logb n. Taking log base a yields the expression in A, with total value j+1. This derivation assumes a,b are held fixed during a recursion chain, even if their initial values depend on the original input size. It does not justify swapping a and b as in C. The rigorous cutoff-based estimate is Θ(1+max(0,loga(logb n))) in the regime n>1,a>1,b>1, with ceiling for integral depth.

Derivation and answer

After j recursive calls the argument is n1/a^j. Equating it to b gives aj=logb n, hence j=loga(logb n) and T(n)=j+1 on exact-depth inputs. With a base cutoff at n≤b, the corresponding depth is max(0, ceil(loga(logb n))) for fixed call parameters a, b>1. This gives intended A when the depth is bounded below away from 0, in particular when it grows. Qualification: the source says a, b are ω(1), not fixed constants, and gives only T(b)=1. Arbitrary inputs need a cutoff/rounding convention, and the additive 1 cannot always be dropped when parameters vary. For example a=n, b=log n gives depth 1 under the cutoff, but loga(logb n) tends to 0. Thus the printed unconditional Theta claim requires a parameter regime/termination convention.

Original paper, page 4 · Answer key

2 marks · MCQ
Report issue

Recursion repeatedly taking the square root

Asymptotic Analysis & Recurrences

Question 45, GATE 2007: What is the time complexity of this recursive function, with unit-cost arithmetic and square root?
int DoSomething(int n) {
    if(n<=2) return 1;
    else return DoSomething(floor(sqrt(n))) + n;
}
Options:
(A) Θ(n²)
(B) Θ(n log₂ n)
(C) Θ(log₂ n)
(D) Θ(log₂ log₂ n)
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Approach. The function makes one recursive call, not n calls. Each square root halves the logarithm of the argument, so analyze the recursion in terms of log n.

2. Derivation and interpretation. Assuming unit-cost integer arithmetic and square root as intended, T(n)=T(floor(sqrt(n)))+Θ(1). After k reductions the argument is approximately n1/2^k, and reaches a constant when 2k=Θ(log n). Thus k=Θ(log log n), giving D. Adding n to the returned value is one arithmetic operation, not n operations; floors change only constant-level stopping details.

Original paper, page 10

2 marks · MCQ
Report issue

Repeated square-root recursion

Asymptotic Analysis & Recurrences

Question 2.11, GATE 2002: For procedure A(n), if n≤2 return 1; otherwise return A(⌈√n⌉). Which listed bound best describes its running time?
(A) O(n); (B) O(log n); (C) O(log log n); (D) O(1).
Question 2.11, GATE 2002: For procedure A(n), if n≤2 return 1; otherwise return A(⌈√n⌉). Which listed bound best describes its running time?
(A) O(n); (B) O(log n); (C) O(log log n); (D) O(1).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: Take logarithms of the argument shrinkage.

Step 2: Let m=log₂n. Taking a square root approximately halves m, so the recursion needs about log₂m halvings to make m constant. Therefore the number of constant-work calls is Θ(log log n). This count concerns runtime, not the returned value, which is always 1. Arbitrary-precision root computation would require a different per-call cost model.

Conclusion: Ignoring integer rounding initially, after k calls the argument is n1/2^k. It becomes bounded once 2k is of order log n, so the recursion depth is Θ(log log n). Ceiling rounding changes only constants, and the small integer arguments reach the base case. With constant-time root/rounding operations per call, C is the tight listed growth bound, even though looser O(n) and O(log n) bounds are also true.

Original paper, page 4