📌  相关文章
📜  当arr [i] = i *(-1)^ i时数组中从索引L到R的元素的总和

📅  最后修改于: 2021-04-22 10:05:14             🧑  作者: Mango

给定两个整数LR和一个数组arr []每个元素在索引处i计算为arr [i] = i *(-1) i 。任务是在索引范围内找到数组的这些元素的总和[L, R]

例子:

天真的方法:根据数组元素的定义,数组的每个奇数元素为负,偶数元素为正。因此,要找到总和,请执行一个从(L到R)的for循环,并保持所有奇数(负)数和偶数(正数)的和。最后,返回总和。

有效方法:可以注意到,该系列所有奇数元素的总和将始终等于(totalOdd) 2 ,其中totalOdd =奇数元素的总数,偶数之和为totalEven *(totalEven + 1) 。现在,我们要做的就是找到直到L和R为止的所有奇数元素的总和。存储两者之差以获得L和R之间所有奇数元素的总和。对偶数进行同样的操作,最后返回偶数和奇数和的差。

下面是上述方法的实现:

C++
// CPP implementation of above approach
#include 
using namespace std;
  
// function to return the odd sum
long int Odd_Sum(int n)
{
  
    // total odd elements upto n
    long int total = (n + 1) / 2;
  
    // sum of odd elements upto n
    long int odd = total * total;
  
    return odd;
}
  
// function to return the even sum
long int Even_Sum(int n)
{
  
    // total even elements upto n
    long int total = (n) / 2;
  
    // sum of even elements upto n
    long int even = total * (total + 1);
  
    return even;
}
  
// Function to find sum from L to R.
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 final sum from L to R
    return even_sum - odd_sum;
}
  
// Driver Program
int main()
{
  
    int L = 1, R = 5;
  
    // function call to print answer
    cout << sumLtoR(L, R);
  
    return 0;
}


Java
// Java implementation of above approach
  
import java.io.*;
  
class GFG {
      
  
  
// function to return the odd sum
static long  Odd_Sum(int n)
{
  
    // total odd elements upto n
    long  total = (n + 1) / 2;
  
    // sum of odd elements upto n
    long  odd = total * total;
  
    return odd;
}
  
// function to return the even sum
static long  Even_Sum(int n)
{
  
    // total even elements upto n
    long  total = (n) / 2;
  
    // sum of even elements upto n
    long  even = total * (total + 1);
  
    return even;
}
  
// Function to find sum from L to R.
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 final sum from L to R
    return even_sum - odd_sum;
}
  
// Driver Program
  
    public static void main (String[] args) {
        int L = 1, R = 5;
  
    // function call to print answer
    System.out.println( sumLtoR(L, R));
    }
}
// This code is contributed by shs..


Python3
# Python3 implementation of above approach
  
# function to return the odd sum
def Odd_Sum(n):
  
    # total odd elements upto n
    total =(n+1)//2
  
    # sum of odd elements upto n
    odd = total*total
    return odd
  
# function to return the even sum
def Even_Sum(n):
  
    # total even elements upto n
    total = n//2
  
    # sum of even elements upto n
    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 final sum from L to R
    return even_sum-odd_sum
  
  
# Driver code
L =1; R = 5
print(sumLtoR(L,R))
  
# This code is contributed by Shrikant13


C#
// C# implementation of above approach
class GFG
{
      
// function to return the odd sum
static long Odd_Sum(int n)
{
  
    // total odd elements upto n
    long total = (n + 1) / 2;
  
    // sum of odd elements upto n
    long odd = total * total;
  
    return odd;
}
  
// function to return the even sum
static long Even_Sum(int n)
{
  
    // total even elements upto n
    long total = (n) / 2;
  
    // sum of even elements upto n
    long even = total * (total + 1);
  
    return even;
}
  
// Function to find sum from L to R.
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 final sum from L to R
    return even_sum - odd_sum;
}
  
// Driver Code
public static void Main () 
{
    int L = 1, R = 5;
  
    // function call to print answer
    System.Console.WriteLine(sumLtoR(L, R));
}
}
  
// This code is contributed by mits


PHP
> 1;
  
    // sum of odd elements upto n
    $odd = $total * $total;
  
    return $odd;
}
  
// function to return the even sum
function Even_Sum($n)
{
  
    // for total even elements upto n
    // divide by 2
    $total = $n >> 1;
      
    // sum of even elements upto n
    $even = $total * ($total + 1);
  
    return $even;
}
  
// Function to find sum from L to R.
function sumLtoR($L, $R)
{
    $odd_sum = Odd_Sum($R) - 
               Odd_Sum($L - 1);
  
    $even_sum = Even_Sum($R) - 
                Even_Sum($L - 1);
  
      
    // print final sum from L to R
    return $even_sum - $odd_sum ;
}
  
// Driver Code
$L = 1 ;
$R = 5;
  
// function call to print answer
echo sumLtoR($L, $R);
  
// This code is contributed by ANKITRAI1
?>


输出:
-3