


If we are given an array ARR=[1,2,3] then the power set P(ARR) of the set ARR is: [ [], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3] ]
For every subset 'X' present in power set P(ARR) of set ARR, X must be sorted i.e. in the example above:
P1(ARR) = [ [], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3] ]
P2(ARR) = [ [], [1], [1,2,3], [2], [1,2], [3], [1,3], [2,3] ]
P3(ARR) = [ [], [1], [2], [1,2], [3], [1,3], [2,3], [2,3,1] ]
P1(ARR) and P2(ARR) will be considered correct power sets but P3(ARR) will not be considered correct because there the last subset [2, 3, 1] is not sorted.
The first line contains a number 'N' denoting the size of the array.
The second line contains 'N' space-separated distinct integer denoting the elements of the array.
For each given 'N' print 2^N separate lines each denoting a subset.
For each subset, print its element separated by space.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= N <= 15
1 <= ARR[i] <= 50
Time limit : 1 second
We will start with a set of empty subsets and will traverse N time. Each time generating new subsets with the addition of the current element and keeping all the previous subsets. So basically we are doubling the size of the set in each iteration.
We can understand the approach with the below example.
We have given N = 3 and input array =[1,2,3]
We will make a recursive function that will help to generate subsets, at each step we have two choices either add the element to the current subset or not and we will call this recursive function. The terminating condition for the recursive function will be when the index of the element is greater than or equal to the size of the given array.
So we have two possibilities either selecting a number in subset or not.
A bit can be either 0 or 1, thus we can use this to denote whether the corresponding element belongs to the current subset or not. So each bit pattern will represent a subset. So we will get all the subsets.
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum