Partition a set into two non | Java Program for above approach ; 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 ; Driver Code ; Given the array ; Length of the array ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static int maxDiffSubsets ( int [ ] arr ) { int totalSum = 0 ; boolean pos = false , neg = false ; int min = Integer . MAX_VALUE ; for ( int 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 ; } public static void main ( String [ ] args ) { int [ ] S = { 1 , 2 , 1 } ; int N = S . length ; if ( N < 2 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( maxDiffSubsets ( S ) ) ; } }