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;
}
}
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)] = 1and the logarithm is 0, yet the loop iterates once).jtakes the values(i+1)*2^kuntil it is at least as large asN, andusing mathematical division. So the update
j *= 2is 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).