Minimum number of coins having value equal to powers of 2 required to obtain N | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count_setbit ( int N ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( 1 << i ) & N ) { result ++ ; } } cout << result << endl ; } int main ( ) { int N = 43 ; count_setbit ( N ) ; return 0 ; }