473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Changing complexity from O(n) to O(1)

For the following code :
Expand|Select|Wrap|Line Numbers
  1.  s = 0 ;
  2.  for(i=m ; i<=(2*n-1) ; i+=m)
  3.       {
  4.             if(i<=n+1)
  5.             { s+=(i-1)/2 ; }
  6.            else
  7.             { s+=(2*n-i+1)/2 ; }  
  8.       }
  9.  
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 ?
May 5 '12 #1
3 1611
Rabbit
12,516 Expert Mod 8TB
I don't understand what your goal is. What is the code supposed to do?
May 5 '12 #2
this is my whole code :
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. int main()
  4. {
  5.    unsigned long int t , n , m , i ;
  6.    unsigned long long int s ,s1 ;
  7.    scanf("%ld",&t);
  8.    while(t--)
  9.    {   s = 0 ;
  10.       scanf("%ld",&n) ;
  11.       scanf("%ld",&m) ; 
  12.       for(i=m ; i<=(2*n-1) ; i+=m)
  13.       {
  14.             if(i<=n+1)
  15.             { s+=(i-1)/2 ;  }
  16.            else
  17.             { s+=(2*n-i+1)/2 ; }  
  18.       }
  19.       printf("s=%lld\n",s); 
  20.    }
  21.    return 0 ;
  22. }
  23.  
Given N and M I wanted to calculate how many pairs a,b(1 <= a < b <=N) are there such that (a+b) is divisible by M . For example when N=4 and M=3, there are 2 possible pairs the sum of which is divisible by M and they are (1,2) and (2,4).

I m using for loop to calculate the sum . I want to eliminate the for loop so that I can calculate the sum in O(1) complexity . So I wanted to derive a formula which would calcutate the sum directly without using for loop . As the sum stores the values like (i-1)/2
I have to calculate the summation of the floor values of (i-1)/2 for (i<=n+1) and summation of the floor values of (2*n-i+1)/2 for (i>n+1) . It became very tedious job as I m uncomfortable with this foor sum . So the formula I derived for summation is not working properly . Can u plz help me out with this problem ? I just want to eliminate the loop so that it can work for larger inputs . Is there any other way to reduce the complexity ? If there is any other way then please elaborate that if I want to eliminate loops what procedure should I follow for future references ?
May 6 '12 #3
donbock
2,426 Expert 2GB
You are seeking a closed-form expression; also called an analytical solution. This is more of a mathematics problem than a programming problem. Perhaps this wikipedia article can help: Closed-form expression. If you derive a formula then I would check it out with paper and pencil before trying to write a program for it.

What do you mean by the term floor value?

You say "I have to calculate the summation of the floor values of (i-1)/2 for (i<=n+1) and the summation of the floor values of (2*n-i+1)/2 for (i>n+1)." You identified two summations, but only one limit for each. Typically a summation goes from one limit to another in discrete steps. What are the limits and step size you want?

By the way, pedantically speaking, it is possible for an O(1) program to contain a for-loop ... as long as the number of times through the loop is indepedent of the input values. This is a harsh condition that makes it hard to imagine a practical use of a for-loop in an O(1) program.
May 6 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: kevin bailey | last post by:
this old script was written when register globals was 'on'. now i need to convert it to run on a server with a php 4.3.6 - do i just convert the relevant variables from $posted_variable to...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
55
by: Ton den Hartog | last post by:
Stupid basic question but I find it horribly imposible to find the answer elsewhere... :-( I want to have a piece of text in my HTML page and want to be able to change it in a Javascript...
4
by: Generic Usenet Account | last post by:
Consider two entities A and B such that there is a 1:n association between them. I mean that associated with each instance of A there are up to n instances of B. Currently in our software we are...
1
by: Mindy Geac | last post by:
Hello, I'm seaching for the possibility to change Domain/User passwords. And a check for users if the password has to change with the first logon or when the password is expired. thanx, ...
8
by: sam_cit | last post by:
Hi, I came to read this particular statement with respect to reducing the complexity of a code, The usage of functional maps matrices instead of switch statements should be considered. I...
4
by: raghu | last post by:
How to find the complexity of a C program? Can anyone explain it with an example... Thanks a lot.
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
10
by: roberto | last post by:
I'm to deploy a .NET DLL which internally communicates with the WS. I don't want others to see internal complexity of the web service classes generated by "Add a Web reference" VS option. As a...
8
by: mike_solomon | last post by:
I have a button <input type="submit" name="Delete" value="Delete"> This code can not be changed I want to use Javascript to change the type I tried:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.