Related Articles

Related Articles

Check if a number can be represented as sum of non zero powers of 2
  • Last Updated : 13 Mar, 2019

Given an integer N, the task is to check whether N can be represented as the sum of powers of 2 where all the powers are > 0 i.e. 20 cannot contribute to the sum.

Examples:

Input: N = 10
Output: 1
23 + 21 = 10

Input: N = 9
Output: 0

Approach: There are two cases:



  1. When N is even then it can always be represented as the sum of powers of 2 when power > 0.
  2. When N is odd then it can never be represented as the sum of powers of 2 if 20 is not included in the sum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that return true if n
// can be represented as the sum
// of powers of 2 without using 2^0
bool isSumOfPowersOfTwo(int n)
{
    if (n % 2 == 1)
        return false;
    else
        return true;
}
  
// Driver code
int main()
{
    int n = 10;
    if (isSumOfPowersOfTwo(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function that return true if n
    // can be represented as the sum
    // of powers of 2 without using 2^0
    static boolean isSumOfPowersOfTwo(int n)
    {
        if (n % 2 == 1)
            return false;
        else
            return true;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 10;
        if (isSumOfPowersOfTwo(n))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function that return true if n 
# can be represented as the sum 
# of powers of 2 without using 2^0
def isSumOfPowersOfTwo(n):
    if n % 2 == 1:
        return False
    else:
        return True
  
# Driver code
n = 10
if isSumOfPowersOfTwo(n):
    print("Yes")
else:
    print("No")
  
# This code is contributed
# by Shrikant13

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG {
  
    // Function that return true if n
    // can be represented as the sum
    // of powers of 2 without using 2^0
    static bool isSumOfPowersOfTwo(int n)
    {
        if (n % 2 == 1)
            return false;
        else
            return true;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 10;
        if (isSumOfPowersOfTwo(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function that return true if n
// can be represented as the sum
// of powers of 2 without using 2^0
function isSumOfPowersOfTwo($n)
{
    if ($n % 2 == 1)
        return false;
    else
        return true;
}
  
// Driver code
$n = 10;
if (isSumOfPowersOfTwo($n))
    echo("Yes");
else
    echo("No");
  
// This code is contributed 
// by Code_Mech
?>

chevron_right


Output:

Yes

Time Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :