Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of the java code

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)

  {
     for (int j = 0; j < i; j++)
           sum++; 
  }

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

like image 298
Dude Avatar asked Feb 20 '23 12:02

Dude


2 Answers

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.

like image 167
Peter Lawrey Avatar answered Feb 23 '23 00:02

Peter Lawrey


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.

like image 28
vidstige Avatar answered Feb 23 '23 01:02

vidstige