- Arrays, Greedy, Mathematical, partition, subset

Maximum possible difference of two subsets of an array | Set 2

  #include using namespace std;  int maxSumAfterPartition(int arr[], int n){        vector pos;          vector neg;          int zero = 0;          int pos_sum = 0;          int neg_sum = 0;          for (int i = 0; i < n; i++) {          if (arr[i] > 0) {              pos.push_back(arr[i]);            pos_sum += arr[i];        }        else if (arr[i] < 0) {              neg.push_back(arr[i]);            neg_sum += arr[i];        }        else {              zero++;        }    }          int ans = 0;              sort(pos.begin(), pos.end());              sort(neg.begin(), neg.end(), greater());              if (pos.size() > 0 && neg.size() > 0) {          ans = (pos_sum – neg_sum);    }    else if (pos.size() > 0) {          if (zero > 0) {                                      ans = (pos_sum);        }        else {                          ans = (pos_sum – 2 * pos[0]);        }    }    else {        if (zero > 0) {                                                                ans = (-1 * neg_sum);        }        else {                                                  ans = (neg[0] – (neg_sum – neg[0]));        }    }      return ans;}int main(){    int arr[] = { 1, 2, 3, -5, -7 };    int n = sizeof(arr) / sizeof(arr[0]);      cout