Our April 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career

Given an array s of n integers, partition it into two non-empty subsets, s1 and s2, such that the sum of all elements in s1 is equal to the sum of all elements in s2. Return a boolean array of size n where i-th element is True if i-th element of s belongs to s1 and False if it belongs to s2. Any valid answer will be accepted. If such partitioning is not possible, return an empty array.

Input: n = 6, s = [10, -3, 7, 2, 1, 3]

Output: [True, True, False, False, False, True]

There are multiple partitionings where s1 sums up to 10 and s2 sums up to 10; they are all correct answers:

1) s1 = [ 10 , -3 , 3 ] and s2 = [ 7 , 2 , 1 ] (Sample output)

2) s1 = [ 7 , 2 , 1 ] and s2 = [ 10 , -3 , 3 ]

3) s1 = [10] and s2 = [-3, 3, 7, 2, 1]

4) s1 = [-3, 3, 7, 2, 1] and s2 = [10]

5) s1 = [10, -3, 2, 1] and s2 = [7, 3]

6) s1 = [7, 3] and s2 = [10, -3, 2, 1]

Input Parameters: The first and only parameter of the function that is to be implemented is the array of integers s, that is to be partitioned.

Output Format: If it is possible to partition the given array s in an above-said manner then return a boolean array of size n, where its i (0

● 1

● -100

We provided two solutions.

This partitioning problem can be reduced to finding a subset that sums up to half of the total sum. Also, if the value of the sum is odd then we cannot partition it into two equal subsets. So, in case the value of the sum is odd we simply return an empty array.

In this approach, we iterate over all possible combinations of subsets of the given array and check if the current subset sums to sum/2. If we find one such subset, we declare this subset s1 (the remaining elements belong to s2 then).

O(n*2^n) where n is the number of elements in the given input array.

As we are iterating on all possible subsets i.e. 2^n subsets for an array of size n. Hence, we are doing O(2^n) iterations and then for each subset, we are computing its sum. To do this we need to iterate over each element of the subset that takes O(n) time of each individual subset. Hence, the total time complexity becomes O(2^n) * O(n) ~ O(n*2^n).

O(n) where n is the number of elements in the given input array. To generate all partitionings we recursively backtrack on all indexes of the array. Call stack might take up to O(n) space.

Apart from this we are only traversing on the given subarray multiple times for different subsets without maintaining any state information, hence we do not allocate any space for processing. The only space we allocate is the final return array that is of size n and hence the total auxiliary space complexity is O(n) + O(n) = O(n).

O(n) where n is the number of elements in the given input array.

Auxiliary space + the Input Space i.e. O(n) + O(n) = O(n).

As discussed in the brute force approach we have simply reduced this problem to a subset sum problem such that given an array s and we need to first check if a subset exists with the subset sum of sum/2. If it exists then we need to separate that subset from the rest of elements of the array. We will be using dynamic programming to solve this problem. Our first aim will be to check if a subset with sum sum/2 exists or not. To do so, we will be maintaining a 2D DP state as following :

Boolean state(idx, sum).

Here, state(idx, sum) tells us if it is possible to get a subset sum of the sum provided the elements from 0 to idx of the given array.

Now, our state transition will look like below:

state(idx, sum) = state(idx - 1, sum) | state(idx - 1, sum - s[idx])

So, using the above state transition we will populate all our DP states. Now, we simply check the value of state(n-1, sum/2) (assumed 0-based array index). If it is true then it is possible to partition the given array and if it is false then once again we return an empty array.

Now, to get the partitioning we start a top-down lookup on our DP states. We start from the state(n-1, sum/2). If this state is true and state(n-2, sum/2) is false this means s[n-1] contributed to the subset sum and if it is false we go to state(n-2, sum/2) to identify our contributors of the subset sum of sum/2. We repeat this reverse DP transition until the point we reach the first index of the array or till the point, the required sum becomes 0. While doing these reverse DP transitions we also mark the contributed elements as s1 subset elements and rest of the array as s2 elements. Because the elements in our array can also be negative and hence we use a hash-based container like unordered_map in C++ to overcome this problem of negative indexing. Kindly, refer to the solution for implementation details.

O(n*range_sum) since this is a pseudo-polynomial time problem where n is the number of elements in the given input array and range_sum is the absolute difference between the maximum sum and the minimum sum possible in the given input array s.

As we are visiting all the DP states i.e. n*range_sum, hence we will be doing n*range_sum iterations and for each state, we are doing O(1) amount of work and also because of memorization each state is being visited once. Hence, the total time complexity of this solution is O(n*range_sum).

O(n*range_sum) where n is the number of elements in the given input array and range_sum is the absolute difference between the maximum sum and the minimum sum possible in the given input array s.

Since we are using an auxiliary container of size n*range_sum to store the DP states. So, the auxiliary space complexity is O(n*range_sum).

O(n*range_sum) where n is the number of elements in the given input array and range_sum is the absolute difference between the maximum sum and the minimum sum possible in the given input array s.

Auxiliary space + the Input space i.e. O(n*range_sum) + O(n) → O(n*range_sum).