Partition Problem in C++

C++Server Side ProgrammingProgramming

In this problem, we must build C++ code to determine whether or not an array may be divided into two equal subarrays. Also, we have to check the condition if the sum of all the elements in both subarrays is exactly the same or not. The partitioning problem is a variant of the Subset Sum Problem, which is in turn a variant of the Knapsack Problem. We'll use the C++ programming language to tackle the partition problem. We must return a string with a Yes or No depending on whether the specified condition is fulfilled or not.

Input

arr[] = {6, 4, 8, 12, 15}

Output

Is possible to divide into two subsets of equal sum

Approach to solve the problem

The goal is to find the sum of all elements in the set. When the array total is odd, we can't split it into two sets. Identify subsets with sum/2 when the total is even. Using the given array, investigate each element one by one, and then choose one of the following choices:

  • Continue to include the current item in the subset, then add the rest to reach the total.

  • Once the current item has been removed from the subset, repeat the process for the other items.

Lastly, return true if the current item is included or excluded in a subset; otherwise, return false. The recursion terminates, if there are no more items or if the sum becomes negative. In the case of a sum of 0, we return true, meaning that a subset has been identified.

Example

#include <bits/stdc++.h>
using namespace std;
bool isSubsetSum(int arr[], int n, int sum) {
   if (sum == 0)
      return true;
   if (n == 0 && sum != 0)
      return false;
   if (arr[n - 1] > sum)
      return isSubsetSum(arr, n - 1, sum);
   return isSubsetSum(arr, n - 1, sum) ||
   isSubsetSum(arr, n - 1, sum - arr[n - 1]);
}
bool findPartiion(int arr[], int n) {
   int sum = 0;
   for (int i = 0; i < n; i++)
      sum += arr[i];
   if (sum % 2 != 0)
      return false;
   return isSubsetSum(arr, n, sum / 2);
}
int main() {
   int arr[] = {
      6,
      4,
      8,
      12,
      15
   };
   int n = sizeof(arr) / sizeof(arr[0]);
   if (findPartiion(arr, n) == true)
      cout << "Is possible to divide into two subsets " "of equal sum";
   else
      cout << "Is impossible to divide into two subsets" " of equal sum";
   return 0;
}

Output

Is impossible to divide into two subsets of equal sum

Approach 2

When the total of the components is not too large, the problem can be handled using dynamic programming. A 2D array part[][] of size (sum/2 + 1)*(n+1) can be created. We may also build the solution from the bottom up, so that each filled entry has the following property. We can solve this issue using a 2-D array of size (sum/2 + 1)*(n + 1) instead of a 2-D array of size (sum/2 + 1)*(n + 1).

Example

#include <bits/stdc++.h>
using namespace std;
bool findPartiion(int arr[], int n) {
   int sum = 0;
   int i, j;
   for (i = 0; i < n; i++)
      sum += arr[i];
   if (sum % 2 != 0)
      return false;
   bool part[sum / 2 + 1];
   for (i = 0; i <= sum / 2; i++) {
      part[i] = 0;
   }
   for (i = 0; i < n; i++) {
      for (j = sum / 2; j >= arr[i]; j--){
         if (part[j - arr[i]] == 1 || j == arr[i])
            part[j] = 1;
      }
   }
   return part[sum / 2];
}
int main() {
   int arr[] = {
      6,
      4,
      8,
      12,
      15
   };
   int n = sizeof(arr) / sizeof(arr[0]);
   if (findPartiion(arr, n) == true)
      cout << "Is possible to divide into two subsets of equal " "sum";
   else
      cout << "Is impossible to divide into two subsets" " of equal sum";
   return 0;
}

Output

Is impossible to divide into two subsets of equal sum

Conclusion

In this problem, we learned how to solve the partition problems along with the c++ code. This code can also be written in java, python, and other languages. We have solved the partition problem using recursive arrays in the C++ programming language. It is a fundamental code but has numerous uses in solving problems.

raja
Updated on 07-Mar-2022 09:29:48

Advertisements