Sum of first N natural numbers with alternate signs
Given an integer N, the task is to find the sum of first N natural numbers with alternate signs, i.e 1 – 2 + 3 – 4 + 5 – 6 + ….
Examples:
Input: N = 6
Output: -3
Explanation:
1 – 2 + 3 – 4 + 5 – 6 = -3
Therefore, the required output is -3.
Input: N = 5
Output: 3
Explanation:
1 – 2 + 3 – 4 + 5 = 3
Therefore, the required output = 3
Naive Approach: Follow the steps below to solve the problem:
- Initialize a variable, say alternateSum to store the sum of alternate sign of first N natural numbers.
- Iterate over the range [1, N] using variable i and check if i is even or not. If found to be true then update alternateSum += -i.
- Otherwise, update alternateSum += i.
- Finally, print the value of alternateSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int alternatingSumOfFirst_N( int N)
{
int alternateSum = 0;
for ( int i = 1; i <= N; i++) {
if (i % 2 == 0) {
alternateSum += -i;
}
else {
alternateSum += i;
}
}
return alternateSum;
}
int main()
{
int N = 6;
cout<<alternatingSumOfFirst_N(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int alternatingSumOfFirst_N( int N)
{
int alternateSum = 0 ;
for ( int i = 1 ; i <= N; i++)
{
if (i % 2 == 0 )
{
alternateSum += -i;
}
else
{
alternateSum += i;
}
}
return alternateSum;
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(alternatingSumOfFirst_N(N));
}
}
|
Python3
def alternatingSumOfFirst_N(N):
alternateSum = 0
for i in range ( 1 , N + 1 ):
if (i % 2 = = 0 ):
alternateSum + = - i
else :
alternateSum + = i
return alternateSum
if __name__ = = "__main__" :
N = 6
print (alternatingSumOfFirst_N(N))
|
C#
using System;
class GFG{
static int alternatingSumOfFirst_N( int N)
{
int alternateSum = 0;
for ( int i = 1; i <= N; i++)
{
if (i % 2 == 0)
{
alternateSum += -i;
}
else
{
alternateSum += i;
}
}
return alternateSum;
}
public static void Main(String[] args)
{
int N = 6;
Console.Write(alternatingSumOfFirst_N(N));
}
}
|
Javascript
<script>
function alternatingSumOfFirst_N(N)
{
var alternateSum = 0;
for (i = 1; i <= N; i++) {
if (i % 2 == 0) {
alternateSum += -i;
}
else {
alternateSum += i;
}
}
return alternateSum;
}
var N = 6;
document.write(alternatingSumOfFirst_N(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
If N is an even number then the sum of alternate sign of first N natural numbers are = (-N) / 2.
If N is an odd number then the sum of alternate sign of first N natural numbers are = (N + 1) / 2.
Follow the steps below to solve the problem:
- Initialize a variable, say alternateSum to store the sum of alternate sign of first N natural numbers.
- Check if N is an even number or not. If found to be true then update alternateSum = (-N) / 2.
- Otherwise, update alternateSum = (N + 1) / 2.
- Finally, print the value of alternateSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int alternatingSumOfFirst_N( int N)
{
int alternateSum = 0;
if (N % 2 == 0) {
alternateSum = (-N) / 2;
}
else {
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
int main()
{
int N = 6;
cout<<alternatingSumOfFirst_N(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int alternatingSumOfFirst_N( int N)
{
int alternateSum = 0 ;
if (N % 2 == 0 )
{
alternateSum = (-N) / 2 ;
}
else
{
alternateSum = (N + 1 ) / 2 ;
}
return alternateSum;
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(alternatingSumOfFirst_N(N));
}
}
|
Python3
def alternatingSumOfFirst_N(N):
alternateSum = 0 ;
if (N % 2 = = 0 ):
alternateSum = ( - N) / / 2 ;
else :
alternateSum = (N + 1 ) / / 2 ;
return alternateSum;
if __name__ = = '__main__' :
N = 6 ;
print (alternatingSumOfFirst_N(N));
|
C#
using System;
class GFG
{
static int alternatingSumOfFirst_N( int N)
{
int alternateSum = 0;
if (N % 2 == 0)
{
alternateSum = (-N) / 2;
}
else
{
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
public static void Main(String[] args)
{
int N = 6;
Console.Write(alternatingSumOfFirst_N(N));
}
}
|
Javascript
<script>
function alternatingSumOfFirst_N(N)
{
var alternateSum = 0;
if (N % 2 == 0) {
alternateSum = (-N) / 2;
}
else {
alternateSum = (N + 1) / 2;
}
return alternateSum;
}
var N = 6;
document.write(alternatingSumOfFirst_N(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
14 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...