Partition a set into two non | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiffSubsets ( int arr [ ] , int N ) { int totalSum = 0 ; bool pos = false , neg = false ; int min = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += 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 ; } int main ( ) { int S [ ] = { 1 , 2 , 1 } ; int N = sizeof ( S ) / sizeof ( S [ 0 ] ) ; if ( N < 2 ) cout << ( " Not ▁ Possible " ) ; else cout << ( maxDiffSubsets ( S , N ) ) ; }