Okay, so I have been wondering what will be the time complexity when the the for loop iterates from 1 to n*n. Can someone please elaborate the time complexity in the following program???
for(i = 1 ; i < n ; i++)
for(j = 1 ; j < i*i ; j++)
for(k = 1 ; k < j ; k++)
Also, a little twist that confuses:
for(i = 1 ; i < n ; i++)
for(j = 1 ; j < i*i ; j++)
if(j%i == 0)
for(k = 1 ; k < j ; k++)
I think the first one is O(n^5)
. j is done i^2 times so does k.
i -> n
j -> n^2
k -> n^2
Thus it is O(n^5)
.
EDIT:
Regarding the second part, maximum number of divisor of a number, N, is less than 2*sqrt(N). Therefore, we can say that upper bound for for(k=1; k < j; k++)
is O(sqrt(j)) = O(n)
.
So, the upper bound for the second part is O(n^4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With