Time Complexity

Here is the code:-

   void function(int n) 
   { 
     int count = 0; 

     for (int i=0; i<n; i++) 
     for (int j=i; j< i*i; j++) 
         if (j%i == 0) 
         { 
            for (int k=0; k<j; k++) 
               // Print statement - O(1) operation 
          } 
    } 

Please explain the time complexity of the above code.

hello @Sakshi2004

the outer loop will run n times.
the second inner loop will run approx n^2 times for each iteration of outer loop
now the third loop will run only when j is multiple of i.
so basically it will run for values j=i, 2i,3i…i * i
in worst case i is n , and i
i will be n^2
so this loop will run approax n^2 ,for each iteration of outer loop(2nd one).
in total we can say
n * n ^ 2 *n ^ 2=> O(n^5)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.