Find maximum subset | Function to calculate maximum sum possible by taking at most K elements that is divisibly by D ; variable to store final answer ; Traverse all subsets ; Update ans if necessary conditions are satisfied ; Input ; Function call
function maximumSum ( A , N , K , D ) { let ans = 0 ; for ( let i = 0 ; i < ( 1 << N ) ; i ++ ) { let sum = 0 ; let c = 0 ; for ( let j = 0 ; j < N ; j ++ ) { if ( i >> j & 1 ) { sum += A [ j ] ; c ++ ; } } if ( sum % D == 0 && c <= K ) ans = Math . max ( ans , sum ) ; } return ans ; } let N = 5 , K = 3 , D = 7 ; let A = [ 1 , 11 , 5 , 5 , 18 ] ; document . write ( maximumSum ( A , N , K , D ) + " " ) ;