Minimum count of numbers required from given array to represent S | Python3 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
import sys NEW_LINE def printAllSubsetsRec ( arr , n , v , Sum ) : NEW_LINE INDENT if ( Sum == 0 ) : NEW_LINE INDENT return len ( v ) NEW_LINE DEDENT if ( Sum < 0 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT x = printAllSubsetsRec ( arr , n - 1 , v , Sum ) NEW_LINE v . append ( arr [ n - 1 ] ) NEW_LINE y = printAllSubsetsRec ( arr , n , v , Sum - arr [ n - 1 ] ) NEW_LINE v . pop ( len ( v ) - 1 ) NEW_LINE return min ( x , y ) NEW_LINE DEDENT def printAllSubsets ( arr , n , Sum ) : NEW_LINE INDENT v = [ ] NEW_LINE return printAllSubsetsRec ( arr , n , v , Sum ) NEW_LINE DEDENT arr = [ 2 , 1 , 4 , 3 , 5 , 6 ] NEW_LINE Sum = 6 NEW_LINE n = len ( arr ) NEW_LINE print ( printAllSubsets ( arr , n , Sum ) ) NEW_LINE