Given a number N ( maybe up to 10^9 ). The task is to find the sum of first N natural number taking powers of 2 as a negative number.
Examples:
Input: N = 4
Output: -4
- 1 - 2 + 3 - 4 = -4
1, 2, and 4 are the powers of two.
Input: N = 5
Output: 1
Approach: An efficient solution is to store the powers of two in an array and then store presum of this array in another array. This array size can be at most 30. So, normally search for the first element in the power array which is greater than the given number.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power[31];
int pre[31];
void PowerOfTwo()
{
int x = 1;
for ( int i = 0; i < 31; i++) {
power[i] = x;
x *= 2;
}
pre[0] = 1;
for ( int i = 1; i < 31; i++)
pre[i] = pre[i - 1] + power[i];
}
int Sum( int n)
{
int ans = n * (n + 1) / 2;
for ( int i = 0; i < 31; i++) {
if (power[i] > n) {
ans -= 2 * pre[i - 1];
break ;
}
}
return ans;
}
int main()
{
PowerOfTwo();
int n = 4;
cout << Sum(n);
return 0;
}
|
C
#include <stdio.h>
int power[31];
int pre[31];
void PowerOfTwo()
{
int x = 1;
for ( int i = 0; i < 31; i++) {
power[i] = x;
x *= 2;
}
pre[0] = 1;
for ( int i = 1; i < 31; i++)
pre[i] = pre[i - 1] + power[i];
}
int Sum( int n)
{
int ans = n * (n + 1) / 2;
for ( int i = 0; i < 31; i++) {
if (power[i] > n) {
ans -= 2 * pre[i - 1];
break ;
}
}
return ans;
}
int main()
{
PowerOfTwo();
int n = 4;
printf ( "%d" ,Sum(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int power[] = new int [ 31 ];
static int pre[] = new int [ 31 ];
static void PowerOfTwo()
{
int x = 1 ;
for ( int i = 0 ; i < 31 ; i++) {
power[i] = x;
x *= 2 ;
}
pre[ 0 ] = 1 ;
for ( int i = 1 ; i < 31 ; i++)
pre[i] = pre[i - 1 ] + power[i];
}
static int Sum( int n)
{
int ans = n * (n + 1 ) / 2 ;
for ( int i = 0 ; i < 31 ; i++) {
if (power[i] > n) {
ans -= 2 * pre[i - 1 ];
break ;
}
}
return ans;
}
public static void main (String[] args) {
PowerOfTwo();
int n = 4 ;
System.out.println( Sum(n));
}
}
|
Python 3
power = [ 0 ] * 31
pre = [ 0 ] * 31
def PowerOfTwo():
x = 1
for i in range ( 31 ):
power[i] = x
x * = 2
pre[ 0 ] = 1
for i in range ( 1 , 31 ):
pre[i] = pre[i - 1 ] + power[i]
def Sum (n):
ans = n * (n + 1 ) / / 2
for i in range ( 31 ) :
if (power[i] > n):
ans - = 2 * pre[i - 1 ]
break
return ans
if __name__ = = "__main__" :
PowerOfTwo()
n = 4
print ( Sum (n))
|
C#
using System;
class GFG
{
static int [] power = new int [31];
static int [] pre = new int [31];
static void PowerOfTwo()
{
int x = 1;
for ( int i = 0; i < 31; i++)
{
power[i] = x;
x *= 2;
}
pre[0] = 1;
for ( int i = 1; i < 31; i++)
pre[i] = pre[i - 1] + power[i];
}
static int Sum( int n)
{
int ans = n * (n + 1) / 2;
for ( int i = 0; i < 31; i++)
{
if (power[i] > n)
{
ans -= 2 * pre[i - 1];
break ;
}
}
return ans;
}
public static void Main ()
{
PowerOfTwo();
int n = 4;
Console.WriteLine(Sum(n));
}
}
|
PHP
<?php
$power = array_fill (0, 31, 0);
$pre = array_fill (0, 31, 0);
function PowerOfTwo()
{
global $power , $pre ;
$x = 1;
for ( $i = 0; $i < 31; $i ++)
{
$power [ $i ] = $x ;
$x *= 2;
}
$pre [0] = 1;
for ( $i = 1; $i < 31; $i ++)
$pre [ $i ] = $pre [ $i - 1] + $power [ $i ];
}
function Sum( $n )
{
global $power , $pre ;
$ans = $n * ( $n + 1) / 2;
for ( $i = 0; $i < 31; $i ++)
if ( $power [ $i ] > $n )
{
$ans -= 2 * $pre [ $i - 1];
break ;
}
return $ans ;
}
PowerOfTwo();
$n = 4;
print (Sum( $n ));
?>
|
Javascript
<script>
power = Array(31).fill(0);
pre = Array(31).fill(0);
function PowerOfTwo()
{
var x = 1;
for (i = 0; i < 31; i++)
{
power[i] = x;
x *= 2;
}
pre[0] = 1;
for (i = 1; i < 31; i++)
pre[i] = pre[i - 1] + power[i];
}
function Sum(n)
{
var ans = n * (n + 1) / 2;
for (i = 0; i < 31; i++)
{
if (power[i] > n)
{
ans -= 2 * pre[i - 1];
break ;
}
}
return ans;
}
PowerOfTwo();
var n = 4;
document.write( Sum(n));
</script>
|
Time Complexity: O(31)
Auxiliary Space: O(31)