All problems

Problem 286

All Non-Empty Subsets

Medium · 20 pts · Array, Backtracking, Bit Manipulation

Problem

You are given N distinct integers. Print every non-empty subset of them, one per line; there are 2^N - 1 such subsets. Write the values of each subset in increasing order. List the subsets in lexicographic order of these sequences, comparing values numerically; when one sequence is a prefix of another, the shorter one comes first. Sort the numbers. Then backtracking produces exactly this order: print the current subset, and then try to extend it with each larger number in turn.

Input

First line: integer N. Second line: N distinct integers.

Output

Print 2^N - 1 lines, one subset per line, values space-separated.

Constraints

1 <= N <= 10; -100 <= A[i] <= 100

Example input

3
3 1 2

Example output

1
1 2
1 2 3
1 3
2
2 3
3

Explanation

Sorted, the numbers are 1 2 3. The subsets starting with 1 come first: 1, 1 2, 1 2 3 and 1 3. Then come 2 and 2 3, and finally 3. That is 7 subsets.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: All Permutations Next: Word Search