Maximum Subset Sum possible by negating the entire sum after selecting the first Array element | Function returns maximum subset sum from the given array = ; Case 2 : Negate values from A [ 1 ] to A [ N - 1 ] ; Include only positives for max subset sum ; Return max sum obtained ; Function to return maximum of the maximum subset sum calculated for the two cases ; Case 1 ; Case 2 ; Modifying the sum ; Including first element ; Negating again ; Return the required answer ; Driver Code
function maxSubset ( A , flag ) { let n = A . length ; let sum = 0 ; if ( flag ) { for ( let i = 1 ; i < n ; i ++ ) A [ i ] = - A [ i ] ; } for ( let i = 1 ; i < n ; i ++ ) { if ( A [ i ] > 0 ) { sum += A [ i ] ; } } return sum ; } function findBest ( A ) { let x = maxSubset ( A , false ) ; let y = maxSubset ( A , true ) ; y = - y ; y += A [ 0 ] ; y = - y ; return Math . max ( x , y ) ; } let A = [ 1 , 10 , 4 , - 6 , 3 ] ; document . write ( findBest ( A ) + " " ) ;