For the given if ARR = [1,1,3],the answer will be [ ],[1],[1,1],[1,3],[3],[1,1,3].
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of elements.
The second line of each test case contains ‘ARR’ array.
For each test case, print all the subsets in each line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
Return the output in sorted format as shown in the sample output.
1 <= T <= 10
1 <= N <= 20.
1 <= ARR[i] <=100
Time limit: 1 sec
If there are N elements in the array, then a total of 2^N subsets are possible as each element have two choices either to include it or not.
How many of them are duplicates?
We can treat the duplicate elements as special elements. For example, if we have duplicate elements (5, 5), instead of treating them as two elements that are duplicate, we can treat it as one special element 5, but this element has more than two choices: you can either NOT put it into the subset, or put ONE 5 into the subset, or put TWO 5s into the subset.
So if A[i] has a frequency of ‘F’, then there are F+1 choices.
Therefore, we are given an array (a1, a2, a3, ..., an) with each of them appearing (k1, k2, k3, ..., kn) times, the number of subsets is (k1+1)(k2+1)...(kn+1).
In this approach,we will first sort the array and find the frequency of each element. If the frequency of element ‘X’ is ‘F’, then we will append no Xs ,1 Xs, 2Xs … F Xs to the end of the already made subsets. We will do the following for all unique elements.
In this approach, we will define a recursive function REC(‘st’,’ CUR’, ’ARR’, ’SUBSETS’) that will recursively build the subsets and store them into ‘SUBSETS’ array. For each call of REC(), we will check if the element is duplicate or not. If not, we will make a recursive call by include the ith element into the ‘CUR’ subset and make a recursive call for st as i+1 and then pop out the element after the recursive call to backtrack.
At last, we will return the ‘SUBSETS’ array.
In this approach, we will iterate the mask from 0 to 2^N and observe its binary representation. If the ith bis are set, it implies ARR[i] is included in the current subset and we will also check if including this element forms a duplicate set. If not we will include the current subset into the ‘SUBSETS’ array.
For eg: if suppose we have array : [ 1, 2, 3, 4] and 'mask' = 5(0101) then 'CUR' represent a set which contains element ['2', '4'].
We run this process for ‘MASK’ equal to 0 to 2^N -1 so that all the possible subsets are checked.
Minimum Number of Deletions and Insertions
Minimum Number of Deletions and Insertions
Can You Print
Prime Digit Sum
Prime Digit Sum
Mario And His Princess
8-Queen Problem