Minimum count of numbers required from given array to represent S | Function to find the count of minimum length of the sequence ; Loop to initialize the array as infinite in the row 0 ; Loop to find the solution by pre - computation for the sequence ; Minimum possible for the previous minimum value of the sequence ; Driver Code
function Count ( S , m , n ) { let table = [ ] ; for ( let i = 0 ; i < m + 1 ; i ++ ) { table [ i ] = [ ] ; for ( let j = 0 ; j < n + 1 ; j ++ ) { table [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= n ; i ++ ) { table [ 0 ] [ i ] = Number . MAX_VALUE - 1 ; } for ( let i = 1 ; i <= m ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = Math . min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return table [ m ] [ n ] ; } let arr = [ 9 , 6 , 5 , 1 ] ; let m = arr . length ; document . write ( Count ( arr , m , 11 ) ) ;