For the following code :
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 ?
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 ; }
}
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 ?
Comment