#C13437. Subsets with Duplicates
Subsets with Duplicates
Subsets with Duplicates
You are given an array of integers, which may contain duplicates. Your task is to generate all possible subsets (the power set) of the given array, ensuring that no duplicate subsets appear in the answer.
Each subset should be sorted in non‐descending order, and the overall list of subsets should be ordered first by the subset's length and then lexicographically. In other words, if S is a subset with elements, then for any two subsets A and B, A comes before B if either |A| < |B|
or |A| = |B|
and A < B
in lexicographic order.
For example, given the input array [1, 2]
, the subsets are: [[], [1], [2], [1, 2]]
and for [1, 2, 2]
, the subsets are: [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]
.
The problem may be formally stated using the following mathematical notation:
\( \text{Given } nums = [a_1,a_2,\dots,a_n], \text{ find } \mathcal{P}(nums) \) such that no duplicate subset exists.
inputFormat
The first line of input contains an integer n
, representing the number of elements in the array.
If n > 0
, the second line contains n
space‐separated integers. If n = 0
, then the array is empty.
outputFormat
Output the list of all subsets in a Python-like list format. Each subset should be represented as a list of integers. For example, the output for an input of 1 2
should be:
[[], [1], [2], [1, 2]]## sample
0
[[]]