SUBSET SUM
The Subset Sum Problem is all about displaying all subsets of the given array with a sum equal to a given sum. It is a variation of the 0-1 knapsack problem. In this, we have to pick items of some weight such that sum of weight of the picked items is equal to the capacity of the knapsack (x).
WORKING
Now we consider the first element and now the required sum is equal to the difference between the target sum and value of first element.
The number of elements is equal to the difference between the total elements and 1.
Leave the 'first' element and now the required sum = target sum.
The number of elements is equal to the difference between the total elements and 1.
Let's understand that how can we solve the problem using recursion. Consider the array which is given below:
sum = 9
result = []
In the above example, we have taken an array, and the empty array named result that stores all the values whose resultant sum is equal to 9.
First element in an array is 3. There are two scenarios:
Now we perform the same select and reject operation on element 4 as it is the first element of the array now.
Now we perform the select and reject operation on element 5.
Consider R-5. It also has two scenarios:
Select the element 2 from the array. Once the element 2 gets selected, the array becomes empty, i.e., arr[] = " ". The sum would be 2-2 equals to 0 and the element 2 gets stored in the result array. The result[] = [3, 4, 2].
Reject the element 2 from the array. Once the element 2 gets rejected, the array becomes empty, i.e., arr[] = " ". The sum would be same as previous, i.e., 2 and the result array would also be same as previous, i.e., [3, 4].
Consider R-4. It has two scenarios:
Select the element 5 from the array. Since we are selecting 5 from the array so array arr would contain the elements 2, i.e., arr = [2]. The sum would be 6-5 equals to 1 and the element 5 gets stored in the result array. The result[] = [3, 5].
Reject the element 5 from the array. Since we are rejecting 5 from the array so array arr would contain the element 2, i.e., arr = [2]. The sum would remain same as previous, i.e., 6 and the result array would be same as previous, i.e., {3}.
Consider S-5. It has two scenarios:
Select the element 2 from the array. Since we are selecting 2 from the array so array arr would be empty, i.e., arr = " ". The sum would be 1-2 equals to -1 and the element 2 gets stored in the result array. The result[] = [3, 5, 2].
Reject the element 2 from the array. Since we are rejecting 2 from the array so array arr would become empty. The sum would remain same as previous, i.e., 1 and the result array would be same as previous, i.e., {3, 5}.
Consider R-5. It has two scenarios:
Select the element 2 from the array. Since we are selecting 2 from the array so array arr would be empty, i.e., arr = " ". The sum would be 6-2 equals to 4 and the element 2 gets stored in the result array. The result[] = [3, 2].
Reject the element 2 from the array. Since we are rejecting 2 from the array so array arr would become empty. The sum would remain same as previous, i.e., 6 and the result array would be same as previous, i.e., {3}.
COMPLEXITY
The above solution may try all subsets of given set in worst case.
Therefore time complexity of the above solution is exponential.