-5

What will be the time complexity of the following code?

    for(i=0;i<=n;i++)
     for(j=0;j<=log i;j++)
      print("hello world");

1 Answers1

2

Summing up the number of times the inner loop is iterating, we get,

log1 + log2 + log3 + log4 + ... + logN 
= log(N!)

And according to Stirling's approximation, log(N!) = O(N x log(N)) So the time complexity is O(NlogN)

Kaidul
  • 15,409
  • 15
  • 81
  • 150