In the book Programming Interviews Exposed it says that the complexity of the program below is O(N), but I don't understand how this is possible. Can someone explain why this is?
int var = 2;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j *= 2) {
var += var;
}
}
Outer loop runs
n
times. Now it all depends on the inner loop.The inner loop now is the tricky one.
Lets follow:
@Daniel Fischer's answer is correct.
I would like to add the fact that this algorithm's exact running time is as follows:
Which means:
You need a bit of math to see that. The inner loop iterates
Θ(1 + log [N/(i+1)])
times (the1 +
is necessary since fori >= N/2
,[N/(i+1)] = 1
and the logarithm is 0, yet the loop iterates once).j
takes the values(i+1)*2^k
until it is at least as large asN
, andusing mathematical division. So the update
j *= 2
is calledceiling(log_2 (N/(i+1)))
times and the condition is checked1 + ceiling(log_2 (N/(i+1)))
times. Thus we can write the total workNow, Stirling's formula tells us
so we find the total work done is indeed
O(N)
.