Minimum removals required such that sum of remaining array modulo M is X | Function to find the minimum elements having sum x ; Initialize dp table ; Pre - compute subproblems ; If mod is smaller than element ; Minimum elements with sum j upto index i ; Return answer ; Function to find minimum number of removals to make sum % M in remaining array is equal to X ; Sum of all elements ; Sum to be removed ; Print answer ; Given array ; Given size ; Given mod and x ; Function call
function findSum ( S , n , x ) { let table = new Array ( n + 1 ) ; for ( var i = 0 ; i < table . length ; i ++ ) { table [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < table . length ; i ++ ) { for ( var j = 0 ; j < table . length ; j ++ ) { table [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= x ; i ++ ) { table [ 0 ] [ i ] = Number . MAX_VALUE - 1 ; } for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= x ; 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 [ n ] [ x ] > n ) ? - 1 : table [ n ] [ x ] ; } function minRemovals ( arr , n , m , x ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } let requied_Sum = 0 ; if ( sum % m < x ) requied_Sum = m + sum % m - x ; else requied_Sum = sum % m - x ; document . write ( findSum ( arr , n , requied_Sum ) ) ; } let arr = [ 3 , 2 , 1 , 2 ] ; let n = arr . length ; let m = 4 , x = 2 ; minRemovals ( arr , n , m , x % m ) ;