Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity when the iteration goes from 1 to i*i

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++)
like image 783
ramnarayanan Avatar asked Dec 02 '22 13:12

ramnarayanan


1 Answers

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)

like image 172
smttsp Avatar answered Dec 07 '22 22:12

smttsp