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 expla
You need a bit of math to see that. The inner loop iterates Θ(1 + log [N/(i+1)])
times (the 1 +
is necessary since for i >= 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 as N
, and
(i+1)*2^k >= N <=> 2^k >= N/(i+1) <=> k >= log_2 (N/(i+1))
using mathematical division. So the update j *= 2
is called ceiling(log_2 (N/(i+1)))
times and the condition is checked 1 + ceiling(log_2 (N/(i+1)))
times. Thus we can write the total work
N-1 N
∑ (1 + log (N/(i+1)) = N + N*log N - ∑ log j
i=0 j=1
= N + N*log N - log N!
Now, Stirling's formula tells us
log N! = N*log N - N + O(log N)
so we find the total work done is indeed O(N)
.