Maximize count of array elements required to obtain given sum | Function that count the maximum number of elements to obtain sum V ; Stores the maximum number of elements required to obtain V ; Base Case ; Initialize all table values as Infinite ; Find the max arr required for all values from 1 to V ; Go through all arr smaller than i ; If current coin value is less than i ; Update table [ i ] ; Return the final count ; Given array ; Given sum V ; Function Call
function maxCount ( arr , m , V ) { let table = [ ] ; table [ 0 ] = 0 ; for ( let i = 1 ; i <= V ; i ++ ) table [ i ] = - 1 ; for ( let i = 1 ; i <= V ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( arr [ j ] <= i ) { let sub_res = table [ i - arr [ j ] ] ; if ( sub_res != - 1 && sub_res + 1 > table [ i ] ) table [ i ] = sub_res + 1 ; } } } return table [ V ] ; } let arr = [ 25 , 10 , 5 ] ; let m = arr . length ; let V = 30 ; document . write ( maxCount ( arr , m , V ) ) ;