Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:55:22+00:00 2026-06-10T00:55:22+00:00

Is the Big-O for the following code O(n) or O(log n)? for (int i

  • 0

Is the Big-O for the following code O(n) or O(log n)?

for (int i = 1; i < n; i*=2)
        sum++;

It looks like O(n) or am I missing this completely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:55:23+00:00Added an answer on June 10, 2026 at 12:55 am

    It is O(logn), since i is doubled each time. So at overall you need to iterate k times, until 2^k = n, and in this case it happens when k = logn (since 2^logn = n).

    Simple example: Assume n = 100 – then:

    iter1: i = 1
    iter2: i = 2
    iter3: i = 4
    iter4: i = 8
    iter5: i = 16
    iter6: i = 32
    iter7: i = 64
    iter8: i = 128 > 100
    

    It is easy to see that an iteration will be added when n is doubled, which is logarithmic behavior, while linear behavior is adding iterations for a constant increase of n.

    P.S. (EDIT): mathematically speaking, the algorithm is indeed O(n) – since big-O notation gives asymptotic upper bound, and your algorithm runs asymptotically “faster” then O(n) – so it is indeed O(n) – but it is not a tight bound (It is not Theta(n)) and I doubt that is actually what you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What will be the Big-O of the following code: int f(int n) { int
I have a html/php form like the following: Form 1: Apple options: -big -medium
I use the following code to append a big dom on a mobile browser
I have the following code but I'm not a big fan of reg exp
Guys, I have the following code that is inside a big while loop that
Given the following code: const int size = 20; char buffer[size]; // From the
I am facing problem in big decimal no. Following code snippet will explain my
In this thread some one commented that the following code should only be used
I am trying to get the correct Big-O of the following code snippet: s
The following code is a simplified example of an issue I am seeing. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.