Sum of alternating sign cubes of first N Natural numbers

Given a number N, the task is to find the sum of alternating sign cubes of first N natural numbers, i.e.,

13 – 23 + 33 – 43 + 53 – 63 + ….

Examples:

Input: N = 2
Output: -7
Explanation:
Required sum = 13 – 23 = -7

Input: N = 3
Output: 20
Explanation:
Required sum = 13 – 23 + 33 = 20



Naive Approach: A simple solution is to solve this problem by iterating over a loop from to N and compute the sum by alternating the sign each time.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to compute
// the sum of cubes with
// alternating sign
  
#include <iostream>
  
using namespace std;
  
// Function to compute sum
// of the cubes with
// alternating sign
int summation(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        if (i % 2 == 1)
            sum += (i * i * i);
        else
            sum -= (i * i * i);
  
    return sum;
}
  
// Driver code
int main()
{
    int n = 3;
    cout << summation(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to compute
// the sum of cubes with
// alternating sign
import java.util.*;
  
class GFG {
  
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int n)
{
    int sum = 0;
      
    for(int i = 1; i <= n; i++)
    {
       if (i % 2 == 1)
           sum += (i * i * i);
       else
           sum -= (i * i * i);
    }
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 3;
  
    System.out.println(summation(n));
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to  
# compute the sum of cubes  
# with alternating sign 
  
# Function to compute sum 
# of the cubes with 
# alternating sign 
def summation(n):
      
    sum = 0
    for i in range(1, n + 1):
        if i % 2 == 1:
            sum = sum + (i * i * i)
        else:
            sum = sum - (i * i * i)
  
    return sum
  
# Driver code 
n = 3
  
print(summation(n))
  
# This code is contributed by ishayadav181

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to compute
// the sum of cubes with
// alternating sign
using System;
class GFG{
  
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int n)
{
    int sum = 0;
      
    for(int i = 1; i <= n; i++)
    {
        if (i % 2 == 1)
            sum += (i * i * i);
        else
            sum -= (i * i * i);
    }
    return sum;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 3;
  
    Console.WriteLine(summation(n));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output:

20

Efficient Approach: The key observation in the problem is that every even number is with a negative sign, that is it used to reduce the overall sum. Therefore if we compute the sum of cubes of even numbers and odd numbers individually, then the overall sum can be computed easily.

  • Count of Even or Odd numbers in first N natural numbers
    => Count(C_o) = \frac{N+1}{2}
    => Count(C_e) = \frac{N}{2}
  • Sum of first Even Terms
    => Sum(S_e) = 2*(C_e*(C_e + 1))^{2}
  • Sum of first Odd Terms
    => Sum(S_o) = C_o^{2} * (2*C_o^{2} - 1)
  • Overall Sum
    => S = S_o - S_e
    => S = C_o^{2} * (2*C_o^{2} - 1) - 2*(C_e*(C_e + 1))^{2}

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to compute
// the sum of cubes with
// alternating sign
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to compute sum
// of the cubes with alternating sign
int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
    int se = 2 * ((ce * (ce + 1))
                  * (ce * (ce + 1)));
    int so = (co * co)
             * (2 * ((co * co)) - 1);
    return so - se;
}
  
// Driver Code
int main()
{
    int n = 3;
    cout << summation(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to compute
// the sum of cubes with
// alternating sign
import java.util.*;
  
class GFG{
  
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
      
    int se = 2 * ((ce * (ce + 1)) *
                  (ce * (ce + 1)));
    int so = (co * co) * (2 * ((co * co)) - 1);
      
    return so - se;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 3;
  
    System.out.println(summation(n));
}
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to compute 
# the sum of cubes with 
# alternating sign 
  
# Function to compute sum of 
# the cubes with alternating sign 
def summation(N):
      
    co = (N + 1) / 2
    co = int(co)
      
    ce = N / 2
    ce = int(ce)
      
    se = 2 * ((ce * (ce + 1)) * 
              (ce * (ce + 1)))
    so = (co * co) * (2 * (co * co) - 1)
      
    return so - se
  
# Driver Code 
n = 3
  
print(summation(n))
  
# This code is contributed by ishayadav181

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to compute
// the sum of cubes with
// alternating sign
using System;
  
class GFG{
  
// Function to compute sum
// of the cubes with
// alternating sign
static int summation(int N)
{
    int co = (N + 1) / 2;
    int ce = (N) / 2;
      
    int se = 2 * ((ce * (ce + 1)) *
                  (ce * (ce + 1)));
    int so = (co * co) * (2 * ((co * co)) - 1);
      
    return so - se;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 3;
  
    Console.WriteLine(summation(n));
}
}
  
// This code is contributed by Rohit_ranjan

chevron_right


Output:

20

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.