Given a number n, we need to check if it can be expressed as 2x + 2y or not . Here x and y can be equal.
Examples :
Input : 24 Output : Yes Explanation: 24 can be expressed as 24 + 23 Input : 13 output : No Explanation: It is not possible to express 13 as sum of two powers of 2.
If we take few examples, we can notice that a number can be expressed in the form of 2^x + 2^y if the number is already a power of 2 ( for n > 1 ) or the remainder we get after subtracting previous power of two from the number is also a power of 2.
Below is the implementation of above idea
C++
// CPP code to check if a number can be // expressed as 2^x + 2^y #include <bits/stdc++.h> using namespace std;
// Utility function to check if // a number is power of 2 or not bool isPowerOfTwo( int n)
{ return (n && !(n & (n - 1)));
} // Utility function to determine the // value of previous power of 2 int previousPowerOfTwo( int n)
{ while (n & n - 1) {
n = n & n - 1;
}
return n;
} // function to check if n can be expressed // as 2^x + 2^y or not bool checkSum( int n)
{ // if value of n is 0 or 1
// it can not be expressed as
// 2^x + 2^y
if (n == 0 || n == 1)
return false ;
// if a number is power of 2
// then it can be expressed as
// 2^x + 2^y
else if (isPowerOfTwo(n)) {
cout << " " << n / 2 << " " << n / 2;
return true ;
}
else {
// if the remainder after
// subtracting previous power of 2
// is also a power of 2 then
// it can be expressed as
// 2^x + 2^y
int x = previousPowerOfTwo(n);
int y = n - x;
if (isPowerOfTwo(y)) {
cout << " " << x << " " << y;
return true ;
}
}
return false ;
} // driver code int main()
{ int n1 = 20;
if (checkSum(n1) == false )
cout << "No" ;
cout << endl;
int n2 = 11;
if (checkSum(n2) == false )
cout << "No" ;
return 0;
} |
Java
// Java code to check if a number // can be expressed as 2^x + 2^y class GFG {
// Utility function to check if
// a number is power of 2 or not
static boolean isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1 )) == 0 );
}
// Utility function to determine the
// value of previous power of 2
static int previousPowerOfTwo( int n)
{
while ((n & n - 1 ) > 1 ) {
n = n & n - 1 ;
}
return n;
}
// function to check if
// n can be expressed as
// 2^x + 2^y or not
static boolean checkSum( int n)
{
// if value of n is 0 or 1
// it can not be expressed as
// 2^x + 2^y
if (n == 0 || n == 1 )
return false ;
// if a number is power of 2
// it can be expressed as
// 2^x + 2^y
else if (isPowerOfTwo(n)) {
System.out.println(n / 2 + " " + n / 2 );
}
else {
// if the remainder after
// subtracting previous power of 2
// is also a power of 2 then
// it can be expressed as
// 2^x + 2^y
int x = previousPowerOfTwo(n);
int y = n - x;
if (isPowerOfTwo(y)) {
System.out.println(x + " " + y);
return true ;
}
}
return false ;
}
// driver code
public static void main(String[] argc)
{
int n1 = 20 ;
if (checkSum(n1) == false )
System.out.println( "No" );
System.out.println();
int n2 = 11 ;
if (checkSum(n2) == false )
System.out.println( "No" );
}
} |
Python3
# Python3 code to check if a number # can be expressed as # 2 ^ x + 2 ^ y # Utility function to check if # a number is power of 2 or not def isPowerOfTwo( n):
return (n and ( not (n & (n - 1 ))) )
# Utility function to determine the # value of previous power of 2 def previousPowerOfTwo( n ):
while ( n & n - 1 ):
n = n & n - 1
return n
# function to check if # n can be expressed as # 2 ^ x + 2 ^ y or not def checkSum(n):
# if value of n is 0 or 1
# it can not be expressed as
# 2 ^ x + 2 ^ y
if (n = = 0 or n = = 1 ):
return False
# if n is power of two then
# it can be expressed as
# sum of 2 ^ x + 2 ^ y
elif (isPowerOfTwo(n)):
print (n / / 2 , n / / 2 )
return True
# if the remainder after
# subtracting previous power of 2
# is also a power of 2 then
# it can be expressed as
# 2 ^ x + 2 ^ y
else :
x = previousPowerOfTwo(n)
y = n - x;
if (isPowerOfTwo(y)):
print (x, y)
return True
else :
return False
# driver code n1 = 20
if (checkSum(n1)):
print ( "No" )
n2 = 11
if (checkSum(n2)):
print ( "No" )
|
C#
// C# code to check if a number // can be expressed as // 2^x + 2^y using System;
class GFG {
// Utility function to check if
// a number is power of 2 or not
static bool isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1)) == 0);
}
// Utility function to determine the
// value of previous power of 2
static int previousPowerOfTwo( int n)
{
while ((n & n - 1) > 1) {
n = n & n - 1;
}
return n;
}
// function to check if
// n can be expressed as
// 2^x + 2^y or not
static bool checkSum( int n)
{
// if value of n is 0 or 1
// it can not be expressed as
// 2^x + 2^y
if (n == 0 || n == 1) {
Console.WriteLine( "No" );
return false ;
}
// if a number is power of
// it can be expressed as
// 2^x + 2^y
else if (isPowerOfTwo(n)) {
Console.WriteLine(n / 2 + " " + n / 2);
return true ;
}
else {
// if the remainder after
// subtracting previous power of 2
// is also a power of 2 then
// it can be expressed as
// 2^x + 2^y
int x = previousPowerOfTwo(n);
int y = n - x;
if (isPowerOfTwo(y)) {
Console.WriteLine(x + " " + y);
return true ;
}
else {
return false ;
}
}
}
// driver code
public static void Main()
{
int n1 = 20;
if (checkSum(n1) == false )
Console.WriteLine( "No" );
Console.WriteLine();
int n2 = 11;
if (checkSum(n2) == false )
Console.WriteLine( "No" );
}
} |
PHP
<?php // PHP code to check if a number // can be expressed as 2^x + 2^y // Utility function to check if // a number is power of 2 or not function isPowerOfTwo( $n )
{ return ( $n and !( $n & ( $n - 1)));
} // Utility function to determine // the value of previous power of 2 function previousPowerOfTwo( $n )
{ while ( $n & $n - 1)
{
$n = $n & $n - 1;
}
return $n ;
} // function to check if // n can be expressed // as 2^x + 2^y or not function checkSum( $n )
{ // if value of n is 0 or 1
// it can not be expressed
// as 2^x + 2^y
if ( $n == 0 or $n == 1)
return false;
// if a number is power of 2
// then it can be expressed
// as 2^x + 2^y
else if (isPowerOfTwo( $n ))
{
echo " " , $n / 2 , " " , $n / 2;
return true;
}
else
{
// if the remainder after
// subtracting previous power
// of 2 is also a power of 2
// then it can be expressed
// as 2^x + 2^y
$x = previousPowerOfTwo( $n );
$y = $n - $x ;
if (isPowerOfTwo( $y ))
{
echo $x , " " , $y ;
return true;
}
}
return false;
} // Driver code $n1 = 20;
if (checkSum( $n1 ) == false)
echo "No" ;
echo "\n" ;
$n2 = 11;
if (checkSum( $n2 ) == false)
echo "No" ;
// This code is contributed // by chandan_jnu. ?> |
Javascript
<script> // javascript code to check if a number // can be expressed as 2^x + 2^y // Utility function to check if
// a number is power of 2 or not
function isPowerOfTwo(n) {
return n != 0 && ((n & (n - 1)) == 0);
}
// Utility function to determine the
// value of previous power of 2
function previousPowerOfTwo(n) {
while ((n & n - 1) > 1) {
n = n & n - 1;
}
return n;
}
// function to check if
// n can be expressed as
// 2^x + 2^y or not
function checkSum(n)
{
// if value of n is 0 or 1
// it can not be expressed as
// 2^x + 2^y
if (n == 0 || n == 1)
return false ;
// if a number is power of 2
// it can be expressed as
// 2^x + 2^y
else if (isPowerOfTwo(n))
{
document.write(n / 2 + " " + n / 2+ "<br/>" );
}
else
{
// if the remainder after
// subtracting previous power of 2
// is also a power of 2 then
// it can be expressed as
// 2^x + 2^y
var x = previousPowerOfTwo(n);
var y = n - x;
if (isPowerOfTwo(y))
{
document.write(x + " " + y + "<br/>" );
return true ;
}
}
return false ;
}
// driver code
var n1 = 20;
if (checkSum(n1) == false )
document.write( "No" );
document.write();
var n2 = 11;
if (checkSum(n2) == false )
document.write( "No" );
// This code is contributed by gauravrajput1. </script> |
Output:
16 4 No
Time Complexity: O(logn)
Auxiliary Space: O(1)