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 1110089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:20:28+00:00 2026-05-17T02:20:28+00:00

I just want to make sure if I am doing this correct. I am

  • 0

I just want to make sure if I am doing this correct. I am trying to count the number of operations performed for the worst case scenario in java

int sum = 0;
    for (int i = 0; i < n; i++ )
    sum++;

Is the number of operations 2+3n or 3+3n?

I got the answer from counting int sum = 0 and int i = 0 for the “2” and i < n, i++, and sum++ as the “3n”. Or is it a 3 rather than a 2 because I have to count i < n before going through the loop?

But either way, is the theta characterization going to be Θ(n)?

Now what if there is a nested for loop like this:

int sum = 0;
    for (int i = 0; i < n; i++ )
        for (int a = 0; a < i; a++)
            sum++;

would it be 3+n*(6a+2) = 6na+2n+3? with Θ(n^2)?

if i change the inner for loop from a < i to a < i*i, would the equation still hold as above or change?

  • 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-05-17T02:20:29+00:00Added an answer on May 17, 2026 at 2:20 am

    Maybe it’s easier to count the number of executions of each statement if there’s only one per line:

    int sum = 0;     // 1 time
    int i = 0;       // 1 time
    while (i < n) {  // n+1 times
      sum++;         // n times
      i++;           // n times
    }
    

    Hence, T(n) = 3*n+3 = Θ(n).

    int sum = 0;       // 1 time
    int i = 0;         // 1 time
    while (i < n) {    // n+1 times
      int a = 0;       // n times
      while (a < i) {  // 1 + 2 + ... + n = n*(n+1)/2 times
        sum++;         // 0 + 1 + ... + n-1 = n*(n-1)/2 times
        a++;           // 0 + 1 + ... + n-1 = n*(n-1)/2 times
      }
      i++;             // n times
    }
    

    Hence, T(n) = 3*n+3 + n*(n-1) + n*(n+1)/2 = Θ(n^2).

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

Sidebar

Related Questions

Sorry for the simple question, just want to make sure I'm doing this right.
I just want to make sure I'm doing this right. This is a simple
I am doing a md5 hash, and just want to make sure the result
I just want to pass this by you and make sure that I am
Sorry if this has already been asked, but I just want to make sure
I just want make sure I'm doing and thinking right about inheritance and constructors
Just want to make sure one thing. In a windows machine (either a desktop
Hi I just want to make sure I have these concepts right. Overloading in
I can't seem to find an answer on this and just want to make
To make sure I'm doing EVERYTHING correct and in hopes of helping someone else

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.