Minimum number of coins that can generate all the values in the given range | C ++ implementation of the approach ; Function to return the count of minimum coins required ; To store the required sequence ; Creating list of the sum of all previous bit values including that bit value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int index ( vector < int > vec , int value ) { vector < int > :: iterator it ; it = find ( vec . begin ( ) , vec . end ( ) , value ) ; return ( it - vec . begin ( ) ) ; } int findCount ( int N ) { vector < int > list ; int sum = 0 ; int i ; for ( i = 0 ; i < 20 ; i ++ ) { sum += pow ( 2 , i ) ; list . push_back ( sum ) ; } for ( i = 0 ; i < 20 ; i ++ ) { if ( list [ i ] >= N ) { return ( index ( list , list [ i ] ) + 1 ) ; } } } int main ( ) { int N = 10 ; cout << findCount ( N ) << endl ; return 0 ; }