Minimum count of numbers required from given array to represent S | C ++ implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the minimum elements required to get the sum of given value S ; Condition if the sequence is found ; Condition when no such sequence found ; Calling for without choosing the current index value ; Calling for after choosing the current index value ; Function for every array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printAllSubsetsRec ( int arr [ ] , int n , vector < int > v , int sum ) { if ( sum == 0 ) { return ( int ) v . size ( ) ; } if ( sum < 0 ) return INT_MAX ; if ( n == 0 ) return INT_MAX ; int x = printAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . push_back ( arr [ n - 1 ] ) ; int y = printAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; return min ( x , y ) ; } int printAllSubsets ( int arr [ ] , int n , int sum ) { vector < int > v ; return printAllSubsetsRec ( arr , n , v , sum ) ; } int main ( ) { int arr [ ] = { 2 , 1 , 4 , 3 , 5 , 6 } ; int sum = 6 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << printAllSubsets ( arr , n , sum ) << endl ; return 0 ; }