Subsets II
mediumRecursion & Backtracking+20 XPTime limit 2s
Like Subsets, but the array may contain duplicates — and the answer must not contain duplicate subsets.
Input:
- line 1:
n - line 2:
nintegers
Output: every distinct subset, one per line, in the form [a b c] (the empty subset is []): numbers inside a subset in increasing order, subsets in lexicographic order (a prefix comes first), exactly as in Subsets.
Constraints: 1 ≤ n ≤ 12, −10 ≤ value ≤ 10.
Think about it: sort, then in the backtracking loop skip a value that equals the previous one at the same depth (k > start and a[k] == a[k−1]). That one line removes every duplicate without a set.
Sample input 1
3 1 2 2
Sample output 1
[] [1] [1 2] [1 2 2] [2] [2 2]
Sample input 2
1 0
Sample output 2
[] [0]
📖 Stuck? Learn the pattern
Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).