I know, the inner loop here is O(log n)
. But, I get confused by the multiplication in the outer loop part.
for (int i = 1; i <= 5*N; i++) {
for (int j = 1; j <= 1000; j = 2*j) {
doSomething(i, j);
}
}
The correct answer to the question is, O(N)
with regard to: how many times does the function doSomething() run? But, I want to know, how this is calculated.
T(n) = O(log n)
, according to: https://www.quora.com/Can-anyone-tell-the-time-complexity-of-for-i-1-to-n-i-i*2-and-how
j=2*j
don't you see it? – John Smith Dec 28 '21 at 16:20j=j+1
it is $O(1)$. It is a constant (i.e. fixed) number of iterations, regardless of the value of $N$. – zyl1024 Dec 28 '21 at 16:24