educative.io

Educative

Is my formula correct for Target Sum

If we are about to find a difference of subsets to be the target sum, like Sum(s1) -Sum(S2) = s, Is the below formula correct?
(Sum(num) - s )/2 = subsetSum.
Total sum of the two subsets = Sum(num)
s = sum of difference between to subsets, (given)
subsetSum = actual knapsack value for a subset.

the actual Idea is to find the smallest value subset with subsetSum and the second subset contains the value subsetSum+s, the difference of these two subsets gives the target sum.

Eg: {1,1,2,3} total sum = 7 , target sum =1.
totalSum-targetSum = 6 ,
subsetSum = 6/2 = 3. Smaller value subset

if we find the subsets which fills smaller value subset , here 3, the remaining subsets should contain subsets with value, smaller value subset + target sum which is 4 here. the difference of these two subsets gives the target sum