0

I had a doubt in computing the time complexity in the following snippet.

Case 1:- for (i = n; i>=1 ; i=i/2) printf("%d", i);

Case 2:- for (i = 1; i < n; i=i*2) printf("%d", i)

Can I tell, the above codes, will take O(N/2) or O(log N) time complexity to run against the input?

Thanks in advance.

1
  • When you run it for various values of n, how many lines do you get? Does the number of lines seem to be proportional to log n? Commented Apr 4, 2016 at 12:50

1 Answer 1

1

It takes O(log2(n)),just think about this.i=1,then print result is 1,then 2,4,8,16 until 2^x>n,then do math in it,x>log2(n),so the time complexity is O(log2(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.