Count ways to represent N as sum of powers of 2 - GeeksforGeeks

26 Mar, 2021
Given an integer N, the task is to count the number of ways to represent N as the sum of powers of 2.
Examples:
Input: N = 4Output: 4Explanation: All possible ways to obtains sum N using powers of 2 are  {4, 2+2, 1+1+1+1, 2+1+1}.
Input: N = 5Output: 4Explanation: All possible ways to obtains sum N using powers of 2 are  {4 + 1, 2+2 + 1, 1+1+1+1 + 1, 2+1+1 + 1}
Naive Approach: The simplest approach to solve the problem is to generate all powers of 2 whose values are less than N and print all combinations to represent the sum N.
Efficient Approach: To optimize the above approach, the idea is to use recursion. Define a function f(N, K) which represents the number of ways to express N as a sum of powers of 2 with all the numbers having power less than or equal to k where K ( = log2(N)) is the maximum power of 2 which satisfies 2K ≤ N.
If (power(2, K) ≤ N) :     f(N, K) = f(N – power(2, K), K) + f(N, K – 1) //to check if power(2, k) can be one of the number.    Otherwise:      f(N, K)=f(N, K – 1)Base cases :
If (N = 0) f(N, K)=1 (Only 1 possible way exists to represent N)
If (k==0) f(N, K)=1 (Only 1 possible way exists to represent N by taking 20)
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for above implementation#include <bits/stdc++.h>using namespace std;int numberOfWays(int n, int k){     // Base Cases    if (n == 0)        return 1;     if (k == 0)        return 1;     // Check if 2^k can be used as    // one of the numbers or not    if (n >= pow(2, k)) {        int curr_val = pow(2, k);        return numberOfWays(n - curr_val, k)               + numberOfWays(n, k - 1);    }    // Otherwise    else         // Count number of  ways to        // N using 2 ^ k - 1        return numberOfWays(n, k - 1);} // Driver Codeint main(){    int n = 4;    int k = log2(n);     cout << numberOfWays(n, k) << endl;}
// Java program to implement// the above approachimport java.util.*;class GFG{static int numberOfWays(int n, int k){     // Base Cases    if (n == 0)        return 1;    if (k == 0)        return 1;     // Check if 2^k can be used as    // one of the numbers or not    if (n >= (int)Math.pow(2, k))    {        int curr_val = (int)Math.pow(2, k);        return numberOfWays(n - curr_val, k)               + numberOfWays(n, k - 1);    }       // Otherwise    else         // Count number of  ways to        // N using 2 ^ k - 1        return numberOfWays(n, k - 1);} // Driver codepublic static void main(String[] args){    int n = 4;    int k = (int)(Math.log(n) / Math.log(2));     System.out.println(numberOfWays(n, k));}} // This code is contributed by susmitakundugoaldanga.
# Python3 program for above implementationfrom math import log2def numberOfWays(n, k):     # Base Cases    if (n == 0):        return 1    if (k == 0):        return 1     # Check if 2^k can be used as    # one of the numbers or not    if (n >= pow(2, k)):        curr_val = pow(2, k)        return numberOfWays(n - curr_val, k) + numberOfWays(n, k - 1)         # Otherwise    else:         # Count number of  ways to        # N using 2 ^ k - 1        return numberOfWays(n, k - 1) # Driver Codeif __name__ == '__main__':    n = 4    k = log2(n)     print(numberOfWays(n, k)) # This code is contributed by mohit kumar 29
// C# program to implement// the above approachusing System;class GFG{static int numberOfWays(int n, int k){     // Base Cases    if (n == 0)        return 1;    if (k == 0)        return 1;     // Check if 2^k can be used as    // one of the numbers or not    if (n >= (int)Math.Pow(2, k))    {        int curr_val = (int)Math.Pow(2, k);        return numberOfWays(n - curr_val, k)               + numberOfWays(n, k - 1);    }       // Otherwise    else         // Count number of  ways to        // N using 2 ^ k - 1        return numberOfWays(n, k - 1);} // Driver codepublic static void Main(String[] args){    int n = 4;    int k = (int)(Math.Log(n) / Math.Log(2));     Console.WriteLine(numberOfWays(n, k));}} // This code is contributed by 29AjayKumar
<script> // JavaScript program for above implementation function numberOfWays(n, k){     // Base Cases    if (n == 0)        return 1;     if (k == 0)        return 1;     // Check if 2^k can be used as    // one of the numbers or not    if (n >= Math.pow(2, k)) {        let curr_val = Math.pow(2, k);        return numberOfWays(n - curr_val, k)            + numberOfWays(n, k - 1);    }    // Otherwise    else         // Count number of ways to        // N using 2 ^ k - 1        return numberOfWays(n, k - 1);} // Driver Code     let n = 4;    let k = Math.log2(n);     document.write(numberOfWays(n, k) + "<br>"); // This code is contributed by Mayank Tyagi </script>
  
4
 
Time Complexity: O((logN+K)K ), where K is log2(N)Auxiliary Space: O(1)
mohit kumar 29
susmitakundugoaldanga
29AjayKumar
mayanktyagi1709
Numbers
Technical Scripter 2020
Combinatorial
Mathematical
Recursion
Mathematical
Recursion
Numbers
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Combinational Sum
Count ways to reach the nth stair using step 1, 2 or 3
Print all possible strings of length k that can be formed from a set of n characters
Count of subsets with sum equal to X
Python program to get all subsets of given size of a set
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays