4

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++) 
3
  • Looks like O(n^4) to me Commented Mar 31, 2016 at 6:05
  • @Ryan Haining Can you please explain? Commented Mar 31, 2016 at 6:12
  • 3
    Actually no that's wrong. The outer loop is n, the middle loop is n*n, the innermost loop goes to n^2 as well s so that's n * n^2 * n^2. I don't feel terribly confident about this though. Commented Mar 31, 2016 at 6:26

4 Answers 4

6

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)

Sign up to request clarification or add additional context in comments.

Comments

5
i = 1..n j = 1..i*i k = 1..j 

WolframAlpha says O(n^5)

2 Comments

In the equation your inner loop should not be over k but 1.
@MateenUlhaq Can you please explain an answer for the one with condition??? if(j%i == 0)
4

The sum to evaluate is

There is an equation for polynomial sums, but the important thing is that the leading term is always one order higher than the polynomial that is being summed. I.e, the answer is O(n^5).

To see the answer directly for loops of type

for(i = 1 ; i < n ; i++) for(j = 1 ; j < i ; j++) 

The correct answer can be calculated by just setting the inner loop to be the worst case,

for(i = 1 ; i < n ; i++) for(j = 1 ; j < n ; j++) 

which gives directly the answer for this e.g., O(n^2).

In the case with the remainder, the condition effects i^2-i cases out of possible i^2. Hence, the loop is effectively just run i times. The inner loop still runs up to n^2. Hence the overall complexity is O(n^4).

Edit: fixed the answer for the remainder case.

2 Comments

So, you claim number of divisors of a number (n^2) is O(n^2)
@smttsp I fixed the answer. I read the code wrong way around first.
3

For the twisted one what I feel is, The outer loop (i iterator) will iterate n times. --> n The j iterator will iterate for 1+2+...n^2 times. So basically making it --> O(n^3) The if condition will hold true for i times each iteration of outer loop (i iteration) because j goes till i * i so the inner loop (k iterator) will execute for n times (same as outer loop; i iterator). And the final code inside inner loop will execute for 1^3+2^3+3^3....n^3 times. Which makes the total complexity O(n^4).

But I don't feel so confident on this either.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.