Equal sum array partition excluding a given element


Problem Statement

For an index and an arr[]. Check if the array[] can be partitioned into two disjoint sets, excluding arr[index] such that the sum of both sets has equal value.

Example 1

Input

arr[] = {4, 3, 1, 2}, Index = 1

Output

No 

Explanation

We have to exclude arr[1] = 3

All possible sets are −

Set 1: (4), Set 2: (2, 1), sum = 4≠3
Set 1: (4,1), Set 2: (2), sum = 5≠2
Set 1: (4,2), Set 2: (1), sum = 6≠1

No combination satisfies the conditions.

Example 2

Input

arr[] = {2, 5, 1, 4, 0}, index = 4

Output

Yes
Set 1 : (2, 4), sum = 6
Set 2 : (5, 1), sum = 6

Solution

This question is a modified version of the partition problem with the added restriction that the index given in the question cannot be included in either of the partitioned sets of the array.

To start, we have to calculate the sum of all the elements of the array except the value at the given index.

Partitioning an array into two equal halves is only possible if the sum is even. There won't be any possible solution if the sum is odd. If the sum comes out to be even, we move forward by defining two variables set1Sum and set2Sum to hold the sum of two sets.

We will use recursion to determine whether set1Sum and set2Sum are equal or not. The starting index is 0 and the array will be traversed recursively. There are two options for every index of an array, either to be included in the set1Sum or set2Sum.

We will call the recursive function for adding the current index in set 1 first and then in set 2. Along with that, make sure to check if the current index is not the one given in the question (which has to be excluded), if it is equal to that index, then call the next position without updating the sums. Once the full array gets traversed, compare the theset1Sum and set2Sum. If the sums are equal then return, otherwise backtrack and check for other options.

Approach

STEP 1 − Define a function, let's name it calcPartitionSum. It will take 6 parameters, a given array(arr), a given number of elements in arr(n), the sum of the first set(set1Sum), the sum of the second set(set2Sum), the index of the element that has to be excluded(index) and the current position in the array(i).

STEP 2 − If i reaches the end of arr, then the function returns true if set1Sum is equal to set2Sum else it returns false.

STEP 3 − If i reaches the index, then the function calls itself with the next i without updating the sums.

STEP 4 − Whenever i is not equal to index, the function will call itself for set1 first and then set 2. In a function call, it will update the value of sum by adding arr[i] to the sum of the corresponding set.

STEP 5 − Define another function, let's name it calcFullSum to calculate the sum of the whole array excluding the value at given index. This function will return false if the sum comes out to be odd, and will call calcPartitionSum.

STEP 6 − The calcPartitionSum function call will have parameters set1Sum and set2Sum initialized to 0, index equal to the given index, and i equal to 0.

STEP 7 − The value returned by the calcPartitionSum will determine the answer.

Example

Below is a C++ program to check if the array[] can be partitioned into two disjoint sets, excluding arr[index] such that the sum of both sets has equal value.

#include <bits/stdc++.h>
using namespace std;
// Function to divide the array into 2 sets and
// to check if the sum of sets becomes equal.
bool calcPartitionSum(int arr[], int n, int set1Sum, int set2Sum, int index, int i){
   // Compare the sums if i reaches the end of array
   // return true if both are equal else return false.
   if (i == n){
      return (set1Sum == set2Sum);
   }
   // If i reaches index, then the function calls
   // itself with the next i without updating the sums
   if (i == index){
      calcPartitionSum(arr, n, set1Sum, set2Sum, index, i + 1);
   }
   // Calling calcPartitionSum by including the value at
   // current index in the set 1
   bool updateSet1 = calcPartitionSum(arr, n, set1Sum + arr[i],set2Sum, index, i + 1);
   // Calling calcPartitionSum by including the value at
   // current index in the set 2
   bool updateSet2 = calcPartitionSum(arr, n, set1Sum, set2Sum + arr[i], index, i + 1);
   // returning true if any one of above calls give a true result
   return  updateSet1 || updateSet2;
}
// Function to check if the array can be divided into 2 sets
// and calls calcPartitionSum accordingly
bool calcFullSum(int arr[], int n, int index){
   // Initiate sum variable with 0
   int sum = 0;
   // Iterate through the whole array and update the sum
   for (int i = 0; i < n; i++) {
      // Not updating the value of sum for given index
      // that needs to be excluded
      if (i == index){
         continue;
      }
      sum += arr[i];
   }
   // If sum is odd return false
   if (sum % 2 != 0) return false;
   // If sum is even call calcPartitionSum function.
   // The parameters set1Sum, set2Sum and i are initiated as 0
   return calcPartitionSum(arr, n, 0, 0, index, 0);
}
// Driver Code
int main() {
   int arr[] = {2, 5, 1, 4, 0};
   int index = 4;
   int n = sizeof(arr) / sizeof(arr[0]);
   if (calcFullSum(arr, n, index)){ 
      cout << "Yes";
   }
   else{
      cout << "No";
   }
   return 0;
}

Output

The above C++ program will produce the following output for given input arr[] = {2, 5, 1, 4, 0} and index = 4 −

Yes

Time Complexity

The time complexity of this algorithm is O(2^n), where n is the number of elements in the array. As the calcPartitionSum is getting called twice, once for set1Sum and once for set2Sum for every position in the array T.C. becomes O(2^n).

Space Complexity

The space complexity of this algorithm is O(n), where n is the number of elements in the array. As calcPartitionSum is a recursive function, it will have a recursive call stack of size n.

Conclusion

So, we solved the Equal sum array partition excluding a given element using recursion. We first check if dividing the array into 2 sets excluding the given index can give a result or not. In case it is possible to divide the array into 2 sets, we check every possible combination of 2 sets that can be created after dividing the array, and return true if any one of that combinations satisfied the given condition.

Updated on: 24-Aug-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements