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); ` `} ` |

*chevron_right*

*filter_none*

(A) (n)

(B) (nlogn)

(C) (logn)

(D) (loglogn)

**(A)** A

**(B)** B

**(C)** C

**(D)** D

**Answer:** **(D)** **Explanation:** Recursive relation for the DoSomething() is

T(n) = T() + 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)

## Recommended Posts:

- Algorithms | Analysis of Algorithms (Recurrences) | Question 1
- Algorithms | Analysis of Algorithms (Recurrences) | Question 2
- Algorithms | Analysis of Algorithms (Recurrences) | Question 3
- Algorithms | Analysis of Algorithms (Recurrences) | Question 4
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 6
- Algorithms | Analysis of Algorithms (Recurrences) | Question 7
- Algorithms | Analysis of Algorithms (Recurrences) | Question 9
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms | Question 1
- Algorithms | Analysis of Algorithms | Question 2
- Algorithms | Analysis of Algorithms | Question 3
- Algorithms | Analysis of Algorithms | Question 4
- Algorithms | Analysis of Algorithms | Question 5
- Algorithms | Analysis of Algorithms | Question 19
- Algorithms | Analysis of Algorithms | Question 19
- Algorithms | Analysis of Algorithms | Question 8
- Algorithms | Analysis of Algorithms | Question 9
- Algorithms | Analysis of Algorithms | Question 10