Take the 2-minute tour ×

I am taking up algorithm course on coursera,and I am stuck on this particular problem. I am supposed to find the time complexity of this code.

int sum = 0

 for (int i = 1; i <= N*N; i = i*2)


I checked it in eclipse itself, for any value of N the number of times sum statement is executed is less than N

final value of sum:
for N=8 sum=3 
for N=16 sum=7 
for N=100000 sum=511

so the time complexity should be less than N but the answer that is given is N raised to the power 2, How is it possible?

What I have done so far:

the first loop will run log(N^ 2) times, and consequently second loop will be execute 1,2,3.. 2 logN

share|improve this question
Why would you even have that inner loop? Why not just sum += i? –  EJP Aug 13 '12 at 1:07
add comment

4 Answers

up vote 3 down vote accepted

The first inner loop will be 1 + 2 + 4 + 8 .. 2^M where 2^M is <= N * N.

The sum of powers of 2 up to N * N is approximately 2 * N * N or O(N ^ 2)

Note: When N=100000 , N*N will overflow so its result is misleading. If you consider overflow to be part of the problem, the sum is pretty random for large numbers so you could argue its O(1), i.e. if N=2^15, N^2 = 2^30 and the sum will be Integer.MAX_VALUE. No higher value of N will give a higher sum.

share|improve this answer
try in an optimizer and see that it is O(1) –  huseyin tugrul buyukisik Aug 12 '12 at 20:06
You are correct. –  Saeed Amiri Aug 12 '12 at 20:12
how is 1 + 2 + 4 + 8 .. 2^M (N^2 ) plz explain this –  Dude Aug 12 '12 at 20:21
The sum of powers of 2 up to 2^M is 2^(M+1) - 1. I knew the proof in high school but don't have much use for mathematical proofs these days. ;) You can see the pattern if you try it. 1, 3, 7, 15, 31, 63, 127, 255 etc. –  Peter Lawrey Aug 12 '12 at 20:24
Roman Dzhabarov's answer gives the formula. –  nhahtdh Aug 12 '12 at 20:26
show 4 more comments

There is a lot of confusion here, but the important thing is that Big-O notation is all about growth rate, or limiting behavior as mathematicians will say. That a function will execute in O(n*n) means that the time to execute will increase faster than, for example n, but slower than, for example 2^n.

When reasoning with big-O notation, remember that constants "do not count". There is a few querks in this particular question.

  • The N*N expression it-self would lead to a O(log n*n) complexity if the loop was a regular for-loop...
  • ...however, the for-loop increment is i = i*2 causing the outer loop to be executed approximately log n and the function would be in O(log n) if the contents of the inner loop run in a time independent of n.
  • But again, the inner-loop run-time depends on n, but it doesn't do n*n runs, rather it does roughly log (n*n)/2 loops. Remembering that "constants don't count" and that we end up in O(n*n).

Hope this cleared things up.

share|improve this answer
Nope, the for-loop is i *= 2. Not i *= i. –  nhahtdh Aug 12 '12 at 20:20
the loop increment isnt i*=i its i*=2 –  Gir Aug 12 '12 at 20:20
yeah, thanks for the correction guys. –  vidstige Aug 12 '12 at 20:21
Your explanation is incorrect, though. If the inner loop is O(1) (independent of n), the outer loop will run in O(log(n)). –  nhahtdh Aug 12 '12 at 20:25
oh missed that, I'm tired. :) Thanks, updated my answer. –  vidstige Aug 12 '12 at 20:34
add comment

So sum ++ will be executed 1 + 2 + 4 + 8 + ... + N*N, total log2(N*N) times. Sum of geometrical progression 1 * (1 - 2 ^ log2(N*N)/(1 - 2) = O(N*N).

share|improve this answer
add comment

Your outer loop is log(N^2)->2*log(N)->log(N), your inner loop is N^2/2->N^2. So, the time complexity is N^2*log(N).

About the benchmark, values with N=8 or N=16 are ridiculous, the time in the loop is marginal in relation with setting JVM, cache fails, and so on. You must:

Begin with biggest N, and check how it evaluate.

Make multiple runs with each value of N.

Think that the time complexity is a measure of how the algorithm works when N becomes really big.

share|improve this answer
How is inner loop N^2/2? I understand the value of outer loop? –  Dude Aug 12 '12 at 19:56
Ok, I was too fast there. I was thinking about arithmetic succession (1 iterations when i=2, 2 iteration when i = 3...) but didn't think that i goes takes values 1, 2, 4, 8, 16... It is a sum of values of a geometric serie, I have to check it again. –  SJuan76 Aug 12 '12 at 19:59
Yes that is what think, the inner loop is not that simple –  Dude Aug 12 '12 at 20:03
1 + 2 = 3 (2^2 -1), 1 + 2 + 4 = 7 (2^3 - 1)... in general, the iterations will be 2^i - 1 for a given i –  SJuan76 Aug 12 '12 at 20:03
so does it give us O(N ^ 2) –  Dude Aug 12 '12 at 20:23
add comment

Your Answer


