Nos dan el entero n y también en la siguiente línea 2*n enteros que representan una serie de progresión aritmética a 1 , a 2 , a 3 …a 2n están en AP. Necesitamos encontrar la suma de a 1 2 – a 2 2 + a 3 2 …. + un 2n-1 2 – un 2n 2 .
Ejemplos:
Input : n = 2 a[] = {1 2 3 4} Output : -10 Explanation : 12 - 22 + 32 42 = -10. Input : n = 3 a[] = {2 4 6 8 10 12} Output : -84
Enfoque simple: uno por uno encontramos la suma del cuadrado de la serie con términos pares negativos y el término impar como término positivo.
C++
// CPP program to find sum of // series with alternate signed // square AP sums. #include <bits/stdc++.h> using namespace std; // function to calculate series sum int seriesSum(int n, int a[]) { int res = 0; for (int i = 0; i < 2 * n; i++) { if (i % 2 == 0) res += a[i] * a[i]; else res -= a[i] * a[i]; } return res; } // Driver Code int main() { int n = 2; int a[] = { 1, 2, 3, 4 }; cout << seriesSum(n, a); return 0; }
Java
// Java program to find sum of // series with alternate signed // square AP sums. import java.io.*; import java.lang.*; import java.util.*; class GFG { // function to calculate // series sum static int seriesSum(int n, int[] a) { int res = 0, i; for (i = 0; i < 2 * n; i++) { if (i % 2 == 0) res += a[i] * a[i]; else res -= a[i] * a[i]; } return res; } // Driver code public static void main(String args[]) { int n = 2; int a[] = { 1, 2, 3, 4 }; System.out.println(seriesSum(n, a)); } }
Python3
# Python3 program to find sum # of series with alternate signed # square AP sums. # Function to calculate series sum def seriesSum(n, a): res = 0 for i in range(0, 2 * n): if (i % 2 == 0): res += a[i] * a[i] else: res -= a[i] * a[i] return res # Driver code n = 2 a = [1, 2, 3, 4] print(seriesSum(n, a)) # This code is contributed by Ajit.
C#
// C# program to find sum of // series with alternate signed // square AP sums. using System; class GFG { // function to calculate // series sum static int seriesSum(int n, int[] a) { int res = 0, i; for (i = 0; i < 2 * n; i++) { if (i % 2 == 0) res += a[i] * a[i]; else res -= a[i] * a[i]; } return res; } // Driver code public static void Main() { int n = 2; int []a = { 1, 2, 3, 4 }; Console.WriteLine(seriesSum(n, a)); } } //This code is contributed by vt_m.
PHP
<?php // PHP program to find sum of // series with alternate signed // square AP sums. // function to calculate // series sum function seriesSum($n, $a) { $res = 0; for ( $i = 0; $i < 2 * $n; $i++) { if ($i % 2 == 0) $res += $a[$i] * $a[$i]; else $res -= $a[$i] * $a[$i]; } return $res; } // Driver Code $n = 2; $a = array(1, 2, 3, 4); echo seriesSum($n, $a); // This code is contributed by anuj_67. ?>
Javascript
<script> // JavaScript program to find sum of // series with alternate signed // function to calculate // series sum function seriesSum(n, a) { let res = 0, i; for (i = 0; i < 2 * n; i++) { if (i % 2 == 0) res += a[i] * a[i]; else res -= a[i] * a[i]; } return res; } // Driver Code let n = 2; let a = [1, 2, 3, 4]; document.write(seriesSum(n, a)); // This code is contributed by code_hunt. </script>
-10
Complejidad del tiempo: O(2*n)
Espacio auxiliar: O(1) ya que usa espacio constante para variables
Enfoque eficiente: Uso de la progresión aritmética Aplicación
Sabemos que la diferencia común d = a 2 – a 1 = a 3 – a 2 = a 4 – a 3
Resultado = a 1 2 – a 2 2 + a 3 2 …. + a 2n-1 2 – a 2n 2
= (a 1 – a 2 )*(a 1 + a 2 ) + (a 3 – a 4 )*(a 3 +a 4 )+….+(a 2n- 1 – un 2n)*(a 2n-1 + a 2n )
Entonces, como la diferencia común es común a la serie, entonces:
(a 1 – a 2 )[a 1 + a 2 + a 3 …a 2n ]
ahora podemos escribir:
(-d)*(Sum of the term of the 2n term of AP) (-d)*[((2*n)*(a1 + a2n))/2] now we know that common difference is : d = (a1 - a2) Then the difference between : g = (a2n - a1) So we can conclude that g = d*(2*n - 1) the we ca replace d by : g/(2*n - 1) So our result becomes : (n/(2*n - 1)) * (a12 - a2n2)
C++
// Efficient CPP program to // find sum of series with // alternate signed square AP sums. #include <bits/stdc++.h> using namespace std; // function to calculate // series sum int seriesSum(int n, int a[]) { return n * (a[0] * a[0] - a[2 * n - 1] * a[2 * n - 1]) / (2 * n - 1); } // Driver code int main() { int n = 2; int a[] = { 1, 2, 3, 4 }; cout << seriesSum(n, a); return 0; }
Java
// Efficient Java program to // find sum of series with // alternate signed square AP sums. import java.io.*; import java.lang.*; import java.util.*; class GFG { static int seriesSum(int n, int[] a) { return n * (a[0] * a[0] - a[2 * n - 1] * a[2 * n - 1]) / (2 * n - 1); } // Driver Code public static void main(String args[]) { int n = 2; int a[] = { 1, 2, 3, 4 }; System.out.println(seriesSum(n, a)); } }
Python3
# Efficient Python3 program # to find sum of series with # alternate signed square AP sums. # Function to calculate # series sum def seriesSum(n, a): return (n * (a[0] * a[0] - a[2 * n - 1] * a[2 * n - 1]) / (2 * n - 1)) # Driver code n = 2 a = [1, 2, 3, 4] print(int(seriesSum(n, a))) # This code is contributed # by Smitha Dinesh Semwal.
C#
// Efficient C# program to find sum // of series with alternate signed // square AP sums. using System; class GFG { static int seriesSum(int n, int[] a) { return n * (a[0] * a[0] - a[2 * n - 1] * a[2 * n - 1]) / (2 * n - 1); } // Driver Code public static void Main() { int n = 2; int []a= { 1, 2, 3, 4 }; Console.WriteLine(seriesSum(n, a)); } } // This code is contributed by anuj_67..
PHP
<?php // Efficient PHP program to // find sum of series with // alternate signed square AP sums. // function to calculate // series sum function seriesSum( $n, $a) { return $n * ($a[0] * $a[0] - $a[2 * $n - 1] * $a[2 * $n - 1]) / (2 * $n - 1); } // Driver code $n = 2; $a = array(1, 2, 3, 4); echo seriesSum($n, $a); // This code is contributed by anuj_67.. ?>
Javascript
<script> // Efficient Javascript program to // find sum of series with // alternate signed square AP sums. // function to calculate // series sum function seriesSum(n, a) { return n * (a[0] * a[0] - a[2 * n - 1] * a[2 * n - 1]) / (2 * n - 1); } // Driver code let n = 2; a = [1, 2, 3, 4]; document.write(seriesSum(n, a)); // This code is contributed by _saurabh_jaiswal. </script>
-10
Complejidad temporal : O(1) desde que se realizan operaciones constantes
Espacio auxiliar: O(1) ya que usa espacio constante para variables
Publicación traducida automáticamente
Artículo escrito por Manish_100 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA