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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:35:41+00:00 2026-05-16T16:35:41+00:00

Just a confusion…. A portion of a C++ sample code is as follows I

  • 0

Just a confusion….
A portion of a C++ sample code is as follows


I just re-edit the whole post. Sorry for any confusion

int i, j;   
i = 0;    // c1
j = 0;    // c2
while (i < n)   // (n+1)c3
{
  i++;   // n c4
   while ( j < i)  // (2+3+....+n+(n+1)c5
   {
     j++;   // (1+2+...+n)c6
   }
  j = 0;  // c7
}

Clearly, c1, c2, and c7 are constant. We are not interested in those, and they don’t really matter in determing the running time.

What I don’t understand is c5 and c6.

Why is it 2+3+4…+n+(n+1) for c5? Why does it start with 2, instead of 1+2+3…+(n+1)???
Note that we can rewrite C5 -> (n*(n-1)/2) + n

For c6, this cominbation can be rewritten as n*(n-1)/2

Initially I thought C6 is n, because C6 depends on two conditions, the first while and second while loop. But since j will always go back to 0, so we are really depending on the first while loop. Because n < n is false, then the j++ wil run maxiumly n-th time.

n = 3

0 < 3, 1, 0 < 1, 1, 0

1 < 3, 2, 0 < 1, 1, 0

2 < 3, 3, 0 < 1, 1, 0

3 < 3 fail.

Can someone please explicitly explain how C5 and C6 are deteremined?
I am sorry if this problem sound dumb to the experts
Thank you!

  • 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-16T16:35:42+00:00Added an answer on May 16, 2026 at 4:35 pm

    Here, you have a running time of 2n. Everytime i is incremented, j is one smaller, so the inner loop is executed exactly once.

    i=0, j=0 // init
    i=1, j=0 // outer loop
    i=1, j=1 // inner loop
    i=2, j=1 // outer loop
    i=2, j=2 // inner loop
    

    More typically, you’d reset j to 0 in the outer loop. In that case, you’d have a runtime of n*(n-1)/2

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

Sidebar

Related Questions

EDIT: Sorry for all the confusion. It was just a stupid thing I missed.
I use Django social-auth (omab version just to avoid any confusion with the other
i just started MVC so have lot of confusion. whenever we call any server
The question is confusing, I know, but I just couldn't put it any other
Simple enough question, I just need to break through one minimal wall of confusion.
I just uncovered this confusion and would like a confirmation that it is what
Okay, I actually don't have code as of yet because i'm just picking out
Just i have a bit of confusion in understanding static reference. incase of instance
I have a confusion on some piece of code. Inside a class I have
HI Just started to investifate Cassandra and have a bit confusion. Could you suggest

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.