Sum of length of two smallest subsets possible from a given array with sum at least K | javascript program for the above approach ; Function to calculate sum of lengths of two smallest subsets with sum >= K ; Sort the array in ascending order ; Stores suffix sum of the array ; Update the suffix sum array ; Stores all dp - states ; Initialize all dp - states with a max1imum possible value ; Base Case ; Traverse the array arr [ ] ; Iterate over the range [ 0 , K ] ; If A [ i ] is equal to at least the required sum j for the current state ; If the next possible state doesn 't exist ; Otherwise , update the current state to the minimum of the next state and state including the current element A [ i ] ; Traverse the suffix sum array ; If suffix [ i ] - dp [ i ] [ K ] >= K ; Sum of lengths of the two smallest subsets is obtained ; Return - 1 , if there doesn 't  exist any subset of sum >= K ; Driver Code
var max1 = 1000000000 ; function MinimumLength ( A , N , K ) { 0 A . sort ( ) ; var suffix = Array ( N + 1 ) . fill ( 0 ) ; var i ; for ( i = N - 1 ; i >= 0 ; i -- ) suffix [ i ] = suffix [ i + 1 ] + A [ i ] ; var dp = new Array ( N + 1 ) ; for ( i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = new Array ( K + 1 ) ; var j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= K ; j ++ ) { dp [ i ] [ j ] = max1 ; } } ; dp [ N ] [ 0 ] = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { for ( j = K ; j >= 0 ; j -- ) { if ( j <= A [ i ] ) { dp [ i ] [ j ] = A [ i ] ; continue ; } if ( dp [ i + 1 ] [ j - A [ i ] ] == max1 ) dp [ i ] [ j ] = max1 ; else dp [ i ] [ j ] = Math . min ( dp [ i + 1 ] [ j ] , dp [ i + 1 ] [ j - A [ i ] ] + A [ i ] ) ; } } for ( i = N - 1 ; i >= 0 ; i -- ) { if ( suffix [ i ] - dp [ i ] [ K ] >= K ) { return N - i ; } } return - 1 ; } var arr = [ 7 , 4 , 5 , 6 , 8 ] ; var K = 13 ; var N = arr . length ; document . write ( MinimumLength ( arr , N , K ) ) ;