# GATE | Sudo GATE 2020 Mock II (10 January 2019) | Question 39

• Last Updated : 09 Jan, 2020

What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2.

```void fun()
{
int i, j;
for (i=1; i<=n; i++)
for (j=1; j<=log(i); j++)
printf("GeeksforGeeks");
}```

(A) Θ(n)
(B) Θ(nLogn)
(C) Θ(n^2)
(D) Θ(n^2(Logn))

Explanation: The time complexity of above function can be written as: Θ(log 1) + Θ(log 2) + Θ(log 3) + . . . . + Θ(log n) which is Θ (log n!)
Order of growth of ‘log n!’ and ‘n log n’ is same for large values of n, i.e., Θ (log n!) = Θ(n log n). So time complexity of fun() is Θ(n log n).

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

The expression Θ(log n!) = Θ(n log n) can be easily derived from following Stirling’s approximation (or Stirling’s formula)

`log n! = n log n - n + O(log(n))`

Option (B) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up