Check if a number can be expressed as 2^x + 2^y
Last Updated :
01 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfTwo( int n)
{
return (n && !(n & (n - 1)));
}
int previousPowerOfTwo( int n)
{
while (n & n - 1) {
n = n & n - 1;
}
return n;
}
bool checkSum( int n)
{
if (n == 0 || n == 1)
return false ;
else if (isPowerOfTwo(n)) {
cout << " " << n / 2 << " " << n / 2;
return true ;
}
else {
int x = previousPowerOfTwo(n);
int y = n - x;
if (isPowerOfTwo(y)) {
cout << " " << x << " " << y;
return true ;
}
}
return false ;
}
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
class GFG {
static boolean isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1 )) == 0 );
}
static int previousPowerOfTwo( int n)
{
while ((n & n - 1 ) > 1 ) {
n = n & n - 1 ;
}
return n;
}
static boolean checkSum( int n)
{
if (n == 0 || n == 1 )
return false ;
else if (isPowerOfTwo(n)) {
System.out.println(n / 2 + " " + n / 2 );
}
else {
int x = previousPowerOfTwo(n);
int y = n - x;
if (isPowerOfTwo(y)) {
System.out.println(x + " " + y);
return true ;
}
}
return false ;
}
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
def isPowerOfTwo( n):
return (n and ( not (n & (n - 1 ))) )
def previousPowerOfTwo( n ):
while ( n & n - 1 ):
n = n & n - 1
return n
def checkSum(n):
if (n = = 0 or n = = 1 ):
return False
elif (isPowerOfTwo(n)):
print (n / / 2 , n / / 2 )
return True
else :
x = previousPowerOfTwo(n)
y = n - x;
if (isPowerOfTwo(y)):
print (x, y)
return True
else :
return False
n1 = 20
if (checkSum(n1)):
print ( "No" )
n2 = 11
if (checkSum(n2)):
print ( "No" )
|
C#
using System;
class GFG {
static bool isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1)) == 0);
}
static int previousPowerOfTwo( int n)
{
while ((n & n - 1) > 1) {
n = n & n - 1;
}
return n;
}
static bool checkSum( int n)
{
if (n == 0 || n == 1) {
Console.WriteLine( "No" );
return false ;
}
else if (isPowerOfTwo(n)) {
Console.WriteLine(n / 2 + " " + n / 2);
return true ;
}
else {
int x = previousPowerOfTwo(n);
int y = n - x;
if (isPowerOfTwo(y)) {
Console.WriteLine(x + " " + y);
return true ;
}
else {
return false ;
}
}
}
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
function isPowerOfTwo( $n )
{
return ( $n and !( $n & ( $n - 1)));
}
function previousPowerOfTwo( $n )
{
while ( $n & $n - 1)
{
$n = $n & $n - 1;
}
return $n ;
}
function checkSum( $n )
{
if ( $n == 0 or $n == 1)
return false;
else if (isPowerOfTwo( $n ))
{
echo " " , $n / 2 , " " , $n / 2;
return true;
}
else
{
$x = previousPowerOfTwo( $n );
$y = $n - $x ;
if (isPowerOfTwo( $y ))
{
echo $x , " " , $y ;
return true;
}
}
return false;
}
$n1 = 20;
if (checkSum( $n1 ) == false)
echo "No" ;
echo "\n" ;
$n2 = 11;
if (checkSum( $n2 ) == false)
echo "No" ;
?>
|
Javascript
<script>
function isPowerOfTwo(n) {
return n != 0 && ((n & (n - 1)) == 0);
}
function previousPowerOfTwo(n) {
while ((n & n - 1) > 1) {
n = n & n - 1;
}
return n;
}
function checkSum(n)
{
if (n == 0 || n == 1)
return false ;
else if (isPowerOfTwo(n))
{
document.write(n / 2 + " " + n / 2+ "<br/>" );
}
else
{
var x = previousPowerOfTwo(n);
var y = n - x;
if (isPowerOfTwo(y))
{
document.write(x + " " + y + "<br/>" );
return true ;
}
}
return false ;
}
var n1 = 20;
if (checkSum(n1) == false )
document.write( "No" );
document.write();
var n2 = 11;
if (checkSum(n2) == false )
document.write( "No" );
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...