
‘N’ = 4, ‘subsetSum’ = {-1, 0, 3, 4, 4, 5, 8, 9}.
Now in this example, the original array was {-1, 4, 5}. As when we calculate the subset sum of this array we will get the given array. The subset of the this array and its sum is:
{} -> sum is 0.
{-1} -> sum is -1.
{4} -> sum is 4.
{5} -> sum is 5.
{-1, 4} -> sum is 3.
{-1, 5} -> sum is 4.
{4, 5} -> sum is 9.
{-1, 4, 5} -> sum is 8.
The first line of input format contains ‘T’, denoting the number of test cases. Then each test case follows.
The first line of each test case contains an integer ‘N’, denoting the number of elements in the original array ‘ARR’.
The second line of the test case contains an array of ‘2 ^ N’ integers denoting the subset-sum array ‘subsetSum’.
For each test case, print an array denoting the original array ‘ARR’.
Output for every query will be printed in a separate line.
You are not required to print anything explicitly. It has already been taken care of. Just implement the functions.
If the path is valid then it will print true else it will print false.
1 <= T <= 10
2 <= ‘N’ <= 15
-5000 <= ‘subsetSum[i]’ <= 5000
Time Limit: 1 second
Let’s take the subset-sum array be {s1, s2, s3… s(2^n)} in ascending order and the original array be {n(k), n(k-1)… n1, z1, z2… z(t), p1, p2… p(q)} in ascending order, where n1 to n(k) are ‘k’ negative integers, z1 to z(t) are ‘t’ zeros and p1 to p(q) are ‘q’ positive integers in the array. Now take a look at the first two numbers of the ‘subsetSum’ array ‘s1’ and ‘s2’. It is obvious that ‘s1’ is the sum of all the negative numbers ‘n1’ to ‘nk’ and ‘s2’ can be either the sum of ‘n2’ to ‘nk’ or ‘p1’ + the sum of ‘n1’ to ‘nk’. Hence on subtracting ‘s2’ with ‘s1’ we will get some value f1. Now, f1 can either be ‘p1’ or ‘n1’. Now we have to check that for every element in the ‘subsetSum’ array there exist a number which on subtracting either ‘p1’ or ’n1’ gives another number that is present in the array if any of the ‘p1’ or ’n1’ does not give a value which is present in the array we will discard it. we will repeat this process till we find all the integers.
The steps are as follows: