You are given a set of n distinct integers. Produce every subset of the set, including the empty subset and the full set.
To make the answer unique, print the subsets in a fixed canonical order:
Line 1: an integer n.
Line 2: n space-separated distinct integers (this line is empty when n = 0).
Line 1: an integer M, the number of subsets (which equals 2^n).
The next M lines: one subset per line, its elements space-separated in ascending order. The empty subset is printed as an empty line.
n values are distinctThree elements
Input
3 3 1 2
Expected
8 1 2 3 1 2 1 3 2 3 1 2 3
Explanation
Sorted the set is {1,2,3}. There are 2^3 = 8 subsets. Ordered by size then lexicographically: the empty subset (blank line), then singletons 1, 2, 3, then pairs 1 2, 1 3, 2 3, then the full set 1 2 3.
Single element
Input
1 5
Expected
2 5
Explanation
With one element there are 2^1 = 2 subsets: the empty subset (blank line) and {5}.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →