Count ways to represent N as sum of powers of 2 | Java program to implement the above approach ; Base Cases ; Check if 2 ^ k can be used as one of the numbers or not ; Otherwise ; Count number of ways to N using 2 ^ k - 1 ; Driver code
import java . util . * ; class GFG { static int numberOfWays ( int n , int k ) { if ( n == 0 ) return 1 ; if ( k == 0 ) return 1 ; 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 ) ; } else return numberOfWays ( n , k - 1 ) ; } public static void main ( String [ ] args ) { int n = 4 ; int k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; System . out . println ( numberOfWays ( n , k ) ) ; } }