Minimum number of coins having value equal to powers of 2 required to obtain N | Java program for above approach ; Function to count of set bit in N ; Stores count of set bit in N ; Iterate over the range [ 0 , 31 ] ; If current bit is set ; Update result ; Driver Code
public class Main { public static void count_setbit ( int N ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & N ) > 0 ) { result ++ ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int N = 43 ; count_setbit ( N ) ; } }