Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i
Given two integers and and an array arr[] every element of which at index is calculated as arr[i] = i * (-1)i. The task is to find the sum of these elements of the array within the index range .
Examples:
Input: L = 1 , R = 5
Output: -3
Sum = (-1) + 2 + (-3) + 4 + (-5) = -3
Input: L = 5 , R = 100000000
Output: 49999998
Naive Approach: According to the definition of array elements, each odd element of the array is negative and even element is positive. So, to find the sum run a for loop from (L to R) and maintain the sum of all the odd(negative) and even(positive) numbers. Finally, return the sum.
Efficient Approach: It can be noted that the sum of all the odd elements of this series will always be equal to (totalOdd)2 where totalOdd = total number of odd elements and sum of the even numbers will be totalEven * (totalEven + 1). Now, all we have to do is to find the sum of all the odd elements upto L and upto R. Store the difference of both to get the sum of all the odd elements between L and R. Do this same for even numbers and finally return the difference of even and odd sums.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long int Odd_Sum( int n)
{
long int total = (n + 1) / 2;
long int odd = total * total;
return odd;
}
long int Even_Sum( int n)
{
long int total = (n) / 2;
long int even = total * (total + 1);
return even;
}
int sumLtoR( int L, int R)
{
long int odd_sum, even_sum;
odd_sum = Odd_Sum(R) - Odd_Sum(L - 1);
even_sum = Even_Sum(R) - Even_Sum(L - 1);
return even_sum - odd_sum;
}
int main()
{
int L = 1, R = 5;
cout << sumLtoR(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long Odd_Sum( int n)
{
long total = (n + 1 ) / 2 ;
long odd = total * total;
return odd;
}
static long Even_Sum( int n)
{
long total = (n) / 2 ;
long even = total * (total + 1 );
return even;
}
static long sumLtoR( int L, int R)
{
long odd_sum, even_sum;
odd_sum = Odd_Sum(R) - Odd_Sum(L - 1 );
even_sum = Even_Sum(R) - Even_Sum(L - 1 );
return even_sum - odd_sum;
}
public static void main (String[] args) {
int L = 1 , R = 5 ;
System.out.println( sumLtoR(L, R));
}
}
|
Python3
def Odd_Sum(n):
total = (n + 1 ) / / 2
odd = total * total
return odd
def Even_Sum(n):
total = n / / 2
even = total * (total + 1 )
return even
def sumLtoR(L,R):
odd_sum = Odd_Sum(R) - Odd_Sum(L - 1 )
even_sum = Even_Sum(R) - Even_Sum(L - 1 )
return even_sum - odd_sum
L = 1 ; R = 5
print (sumLtoR(L,R))
|
C#
class GFG
{
static long Odd_Sum( int n)
{
long total = (n + 1) / 2;
long odd = total * total;
return odd;
}
static long Even_Sum( int n)
{
long total = (n) / 2;
long even = total * (total + 1);
return even;
}
static long sumLtoR( int L, int R)
{
long odd_sum, even_sum;
odd_sum = Odd_Sum(R) - Odd_Sum(L - 1);
even_sum = Even_Sum(R) - Even_Sum(L - 1);
return even_sum - odd_sum;
}
public static void Main ()
{
int L = 1, R = 5;
System.Console.WriteLine(sumLtoR(L, R));
}
}
|
PHP
<?php
function Odd_Sum( $n )
{
$total = ( $n + 1) >> 1;
$odd = $total * $total ;
return $odd ;
}
function Even_Sum( $n )
{
$total = $n >> 1;
$even = $total * ( $total + 1);
return $even ;
}
function sumLtoR( $L , $R )
{
$odd_sum = Odd_Sum( $R ) -
Odd_Sum( $L - 1);
$even_sum = Even_Sum( $R ) -
Even_Sum( $L - 1);
return $even_sum - $odd_sum ;
}
$L = 1 ;
$R = 5;
echo sumLtoR( $L , $R );
?>
|
Javascript
<script>
function Odd_Sum(n)
{
var total = parseInt((n + 1) / 2);
var odd = total * total;
return odd;
}
function Even_Sum(n)
{
var total = parseInt((n) / 2);
var even = total * (total + 1);
return even;
}
function sumLtoR(L, R)
{
var odd_sum, even_sum;
odd_sum = Odd_Sum(R) - Odd_Sum(L - 1);
even_sum = Even_Sum(R) - Even_Sum(L - 1);
return even_sum - odd_sum;
}
var L = 1, R = 5;
document.write(sumLtoR(L, R));
</script>
|
Complexity Analysis:
- Time Complexity: O(1)
- Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...