Skip to content
Related Articles

Related Articles

Sum of series with alternate signed squares of AP
  • Difficulty Level : Hard
  • Last Updated : 15 May, 2018

We are given the Integer n and also in the next line 2*n integers which represent a Arithmetic Progression series a1, a2, a3…a2n they are in AP. We need to find the sum of a12 – a22 + a32…. + a2n-12 – a2n2 .

Examples :

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

Simple Approach : We one by one find the sum of the square of the series with even terms negative and odd term as positive term .

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 seiresSum(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 << seiresSum(n, a);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 seiresSum(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(seiresSum(n, a));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find sum
# of series with alternate signed  
# square AP sums.
  
# Function to calculate series sum
def seiresSum(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(seiresSum(n, a))
  
# This code is contributed by Ajit.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find sum of 
// series with alternate signed  
// square AP sums.
using System;
  
class GFG 
{
  
    // function to calculate 
    // series sum
    static int seiresSum(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(seiresSum(n, a));
    }
}
  
//This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find sum of 
// series with alternate signed  
// square AP sums.
  
// function to calculate 
// series sum
function seiresSum($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 seiresSum($n, $a);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output :

-10

Efficient Approach:Use of Arithmetic progression Application

We know that common difference d = a2 – a1 = a3 – a2 = a4 – a3



Result = a12 – a22 + a32…. + a2n-12 – a2n2

= (a1 – a2)*(a1 + a2) + (a3 – a4)*(a3 +a4)+….+(a2n-1 – a2n)*(a2n-1 + a2n)

So as common difference is common to the series then :
(a1 – a2)[a1 + a2 + a3…a2n]

now we can write :

(-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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 seiresSum(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 << seiresSum(n, a);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 seiresSum(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(seiresSum(n, a));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

   
# Efficient Python3 program  
# to find sum of series with  
# alternate signed square AP sums.
  
# Function to calculate
# series sum
def seiresSum(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(seiresSum(n, a)))
  
# This code is contributed 
# by Smitha Dinesh Semwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient C# program to find sum 
// of series with alternate signed 
// square AP sums.
using System;
  
class GFG 
{
    static int seiresSum(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(seiresSum(n, a));
    }
}
  
// This code is contributed by anuj_67..

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Efficient PHP program to 
// find sum of series with 
// alternate signed square AP sums.
  
// function to calculate
// series sum
function seiresSum( $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 seiresSum($n, $a);
      
// This code is contributed by anuj_67..
?>

chevron_right


Output :

-10

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :