Fastrail08

Minimum Subset Difference

May 11th, 2025 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /* An optimization can be added in the code:
  5.  
  6.  RULES: If the original array is of size n
  7.  n: even
  8.  each set should be of size: n/2
  9.  
  10.  n: odd
  11.  one of the set would be of size n / 2, and other would be of (n / 2) + 1
  12.  
  13.  So while making recursive calls, we can track the size of the set, if the size of the current set is less than (n + 1)/2 then only we can add a number in the set and call further
  14.  
  15.  so for n = 6
  16.  (n + 1) / 2 = 3
  17.  so either of the set will include the current number only if the size of that set < 3 because if something is added here, no solutions will be formed moving further into the branch as we added more numbers that were required to one of the sets and it would violate the size difference rule.
  18.  
  19.  for n = 7
  20.  (n + 1) / 2 = 4
  21. Either of the set will include the current number only if the size is of that set at the time of calling is < 4. for a set with size = 3, it can call, it size would increase to 4, and now the other set would automatically be of size 3. Same analogy follows as for n = 6.
  22. */
  23. void minSubsetSumDiff(int index, int sum1, int sum2, int &minDiff, vector<int> &v, vector<int> &v1, vector<int> &v2, vector<vector<int> > &result){
  24.     //base case
  25.     if(index >= v.size()){
  26.         /*
  27.         1. n = even, both subset of same size
  28.         2. n = odd, difference between the subset size should be 1
  29.         */
  30.         if((int) v.size() % 2 == 0){
  31.             if((int) v1.size() == (int) v.size() / 2){
  32.                 if(abs(sum1 - sum2) < minDiff){
  33.                     minDiff = abs(sum1 - sum2);
  34.                     while(!result.empty()){
  35.                         result.pop_back();
  36.                     }
  37.                     result.push_back(v1);
  38.                     result.push_back(v2);
  39.                 }
  40.             }
  41.         }
  42.         else{
  43.             if(abs((int) v1.size() - (int) v2.size()) == 1 && abs(sum1 - sum2) < minDiff){
  44.                 minDiff = abs(sum1 - sum2);
  45.                     while(!result.empty()){
  46.                         result.pop_back();
  47.                     }
  48.                     result.push_back(v1);
  49.                     result.push_back(v2);
  50.             }
  51.         }
  52.         return;
  53.     }
  54.    
  55.     // level / current element chooses to go into the left partition
  56.     v1.push_back(v[index]);
  57.     minSubsetSumDiff(index + 1, sum1 + v[index], sum2, minDiff, v, v1, v2, result);
  58.     v1.pop_back();
  59.    
  60.     // level / current element chooses to go into the right partition
  61.     v2.push_back(v[index]);
  62.     minSubsetSumDiff(index + 1, sum1, sum2 + v[index], minDiff, v, v1, v2, result);
  63.     v2.pop_back();
  64. }
  65.  
  66. int main() {
  67.     // your code goes here
  68.     int n;
  69.     cin >> n;
  70.     vector<int> v(n);
  71.     for(int i = 0; i < n; i++){
  72.         cin >> v[i];
  73.     }
  74.     vector<int> v1, v2;
  75.     vector<vector<int> > result;
  76.     int minDiff = INT_MAX;
  77.     minSubsetSumDiff(0, 0, 0, minDiff, v, v1, v2, result);
  78.     for(int i = 0; i < result.size(); i++){
  79.         cout << '[';
  80.         for(int j = 0; j < result[i].size(); j++){
  81.             cout << result[i][j] << ',';
  82.         }
  83.         cout << "]\n";
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment