Partition a set into two non | Function to return the maximum difference between the subset sums ; Stores the total sum of the array ; Checks for positive and negative elements ; Stores the minimum element from the given array ; Traverse the array ; Calculate total sum ; Mark positive element present in the set ; Mark negative element present in the set ; Find the minimum element of the set ; If the array contains both positive and negative elements ; Otherwise ; Given the array ; Length of the array ; Function Call
function maxDiffSubsets ( arr ) { var totalSum = 0 ; var pos = false , neg = false ; var min = Number . MAX_VALUE ; for ( i = 0 ; i < arr . length ; i ++ ) { totalSum += Math . abs ( arr [ i ] ) ; if ( arr [ i ] > 0 ) pos = true ; if ( arr [ i ] < 0 ) neg = true ; if ( arr [ i ] < min ) min = arr [ i ] ; } if ( pos && neg ) return totalSum ; else return totalSum - 2 * min ; } var S = [ 1 , 2 , 1 ] ; var N = S . length ; if ( N < 2 ) document . write ( " " ) ; else document . write ( maxDiffSubsets ( S ) ) ;