Check if a number can be expressed as 2^x + 2^y

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
      
# 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

Output:

16 4
No


My Personal Notes arrow_drop_up

I am a selfmotivated and passionate programmer who loves to code and solve programming challenges

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : Chandan_Kumar




Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions