Minimum count of numbers required from given array to represent S | Java 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 java . util . * ; import java . lang . * ; class GFG { static int printAllSubsetsRec ( int arr [ ] , int n , ArrayList < Integer > v , int sum ) { if ( sum == 0 ) { return ( int ) v . size ( ) ; } if ( sum < 0 ) return Integer . MAX_VALUE ; if ( n == 0 ) return Integer . MAX_VALUE ; int x = printAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . add ( arr [ n - 1 ] ) ; int y = printAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; v . remove ( v . size ( ) - 1 ) ; return Math . min ( x , y ) ; } static int printAllSubsets ( int arr [ ] , int n , int sum ) { ArrayList < Integer > v = new ArrayList < > ( ) ; return printAllSubsetsRec ( arr , n , v , sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 3 , 5 , 6 } ; int sum = 6 ; int n = arr . length ; System . out . println ( printAllSubsets ( arr , n , sum ) ) ; } }