retagged by
447 views

2 Answers

0 votes
0 votes
Outermost loop runs n times.

Loop from j=1 to i*i runs n^2 times

Now for the innermost loop with variable k, we're given a condition. On closely examining it, we can say that the if condition will be true n times. How many times does the loop execute? Since it runs from k=1 till j (which in turn runs till n^2) we might think it will run n^2 times too. But this is wrong. Here k=1 to j will be considered as running a single loop till n (i.e. even though j runs n^2 times but for 'k' in k=1 till j, 'j' will be n not n^2). So complexity will be n*n^2*n = n^4

 

A more convincing answer can be this one:

Since we know the "if" condition is true n times and if we assume "k loop" goes from 1 till n^2, we can keep the "k loop" outside the "j loop" and make it run it n times. As we've assumed it runs from 1 till n^2, the complexity will be: n*( n^2 + n*n^2) = n^4.

First n^2 is for j loop and second for k loop which is executed k times (or we can say it is called n times to run from 1 till n^2)
edited by

Related questions

1 votes
1 votes
1 answer
1
1 votes
1 votes
1 answer
2
2 votes
2 votes
1 answer
3
1 votes
1 votes
1 answer
4