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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:57:47+00:00 2026-06-03T06:57:47+00:00

For the following code : s = 0 ; for(i=m ; i<=(2*n-1) ; i+=m)

  • 0

For the following code :

 s = 0 ;  
 for(i=m ; i<=(2*n-1) ; i+=m)  {  
      if(i<=n+1){ 
        s+=(i-1)/2 ; 
      }  
      else{ 
        s+=(2*n-i+1)/2 ; 
      }    
 }

I want to change the complexity of the code from O(n)
to O(1) . So I wanted to eliminate the for loop . But as
the sum s stores values like (i-1)/2 or (2*n-i+1)/2 so eliminating the loop involves tedious calculation of floor value of each (i-1)/2 or (2*n-i+1)/2 . It became very difficult for me to do so as I might have derived the wrong formula in sums of floors . Can u please help me in Changing complexity from O(n) to O(1). Or please help me with this floor summations . Is there any other way to reduce the complexity ? If yes … then how ?

  • 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-03T06:57:48+00:00Added an answer on June 3, 2026 at 6:57 am

    As Don Roby said, there is a plain old arithmetic solution to your problem. Let me show you how to do it for the first values of i.

    * EDIT 2 : CODE FOR THE LOWER PART *

        for(int i=m ; i<= n+1 ; i+=m)//old computation
              s+=(i-1)/2 ;
    
    
        int a = (n+1)/m; // maximum value of i
        int b = (a*(a+1))/2; //
        int v = 0;
        int p;
        if(m % 2 == 0){
            p = m/2;
            v = b*p-a; // this term is always here
        }
        else{
            p = (m - 1)/2;
            int sum1 = ((a/2)*(a/2 +1))/2; 
            int sum2 =  (((a-1)/2)*((a-1)/2 +1))/2;  
    
            v = b*p -a ;// this term is always here
            v+= sum1 + a/2; //sum( 1 <= j <= a )(j-1), j pair
            v+= sum2; //sum( 1 <= j <= a )(j-1), j impair
        }
        System.out.println( " Are both result equals ? "+ (s == v));
    

    How do I come up with it? I take

     for(i=m ; i<= n+1 ; i+=m)
          s+=(i-1)/2 ;
    

    I make a change

     for(j=1 ; j*m <= n-1 ; j++)
         s+=(j*m-1)/2 ;
    

    I pose a=Math.floor(n+1/m). There are 3 cases :

    1. m is pair, then interior of the loop is s+= p*j. The result is

       b(a*(a+1))/2 -a
      
    2. m is impair and the iterator j is pair

    3. m is impair and the iterator j is impair
      When m is impair, you can write m = 2p + 1 and the interior of the loop becomes

       s+= p*j + (j-1)/2
      

    p*j is the same as before, now you need to break the division by assuming j is always pair or j always impair and summing both values.

    The next loop you need to compute is

     for(int i=a+1 ; i<= (2*n-1) ; i+=m)// a is (n+1)/m
        s+=(2*n-i+1)/2;
    

    which is the same as

     for(int i=1 ; i<= (2*n-1)-a ; i+=m)
        s+= (2n-a)/2 - (i-1)/2;
    

    This loop is similar to the first one, so there is not much work to do…
    Indeed this is tedious..

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

Sidebar

Related Questions

In the following code example, I want to change the color of the Foreground
following code work fine but 1 error $(document).ready( function() { $(#myCheckboxes input[type='checkbox']).change(function() { var
I have the following code. I want to add/remove check-box values to an array
I have the following code snippet: #ifdef DO_LOG #define log(p) record(p) #else #define log(p)
How can I add ELSE clause to the following code? ROW_NUMBER() OVER (ORDER BY
I came across the following expression in someone else's code. I think it's terrible
Following code is from a sample of playframework-2.0: /** * Display the dashboard. */
Following code is to save image took from camera into photo album. if ([mediaType
In the following code I want to reduce these 5 functions down to 3.
Put simply: I want the following code to print sub: Element e = new

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.