1

What is Running Time in Big oh notation of:

for(int i=1;i<N;i++)

    for(int j=1;j<N;j*=2)

The loop will stop when j > N. If we let k be some arbitrary iteration of the loop, the value of j on iteration k will be 2k. The loop stops when 2k > n, which happens when k > log2 n.

Therefore, the number of iterations is only O(log n), so the total complexity is O(log n).

Is this correct?

1

1 Answer 1

4

The O(n) for the outer loop and O(log(n)) for the inner one. So the total is O(n*log(n)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.