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

  • Home
  • SEARCH
  • 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 9143127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:56:47+00:00 2026-06-17T09:56:47+00:00

for i <- 1 to n for j <- i to n for k

  • 0
for i <- 1 to n 
  for j <- i to n
    for k <- i to j

What is the mathematical expression for the run time of the second loop?

Is it the Sum of j = 1 to n of n = n^2?

How do you derive it since its dependent on both i and n?

  • 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-17T09:56:48+00:00Added an answer on June 17, 2026 at 9:56 am

    Let’s approach the problem in two ways:

    1. solution

    If you need to compute the second loop run time function, you will have to express it as a function of both n and i. So, you will fix i, because it enters the second loop as a constant, and you don’t change its value anywhere in the 2nd or 3rd loop. You can write your algorithm in an equivalent form, if you want to see why the second loop running time depends on both i and n:

    for i <- 1 to n 
       doSomething(i,n)
    
    
      doSomething(i,n):
        for j <- i to n
         for k <- i  to j
           doStuffHere() //
    

    When j=i, doStuff() is executed only once, because j=i and k=j=i, so the loop stops after just 1 iteration.

    When j=i+1, doStuff() is executed 2 times, etc.

    You can derive a rule, that for any value of j, doStuff() is executed (j-i+1) times, i.e. 1 time when j=i, 2 times when j=i+1, 3 times when j=i+3,…, n-i+1 times when j=i. That means that the running time function is:

    f(n,i) = 1+2+3+...+(n-i+1) = (n-i+1)*(n-i+1+1)/2 =
           = (n-i+1)(n-i+2)/2 
    

    Later, when you try to derive the complexity function of the entire algorithm, you will see that for each i from 1 to n, you have f(n,i) doStuff(), so the whole algorithm will have the running time:

    RT(n) = f(1,n) + f(2,n) + f(3,n) + .. + f(n,n)
    

    You will have to do some maths here and use formulas for progression sums. However, there is another approach, far more elegant, but maybe tricky to understand.

    2. solution

    It is evident that for every j and k in your algorithm:

    • i <= j,k <= n
    • k >= j

      So, what your code does is actually choosing two positive integers j and k from [i,n] where j<=k. Set containing those pairs j,k is actually a combination with repetition where we choose two elements from n-i+1 elements with repetition. Don’t get confused because in some choices the first number won’t be less than the second one. You can always switch them, i.e. rewrite them in the non descending order, and then consider the first to be your j, and the second one to be your k. Those two numbers can be selected in (n-i+1)(n-i+2)/2 ways, and that is the running time function for your algorithm. With more thinking, and less error prone math calculations you can get your solution in a far more elegant way.
      The same logic applies if you try to determine the running time function of the entire algorithm.

      Useful links with math formulas

    • http://en.wikipedia.org/wiki/Combinations

    • http://en.wikipedia.org/wiki/Arithmetic_progression
    • there are more links, but since I’m a new user here, I’m not allowed to post more than two hyperlinks before I earn 10 reputation points. : )
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I need a mathematical expression for the following loop, but I can't seem
I want to validate mathematical expressions using regular expression. The mathematical expression can be
Possible Duplicate: Evaluating a string as a mathematical expression in javascript I have written
I have a Binary tree for a mathematical expression(infix), i want to convert directly
How do I parse and evaluate a mathematical expression in a string (e.g. '1+1'
I'm trying to use a Python regex to find a mathematical expression in a
Can anyone show me how to create a mathematical expression for the number of
I'm trying to make a Javascript function that will take a mathematical expression and
I have a large mathematical expression that has to be created dynamically. For example,
I'm looking for the mathematical expression converting a 3D coordinate (x0,y0,z0) to a 2D

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.