Loading content...
You are given an array of integers called candidates (which may contain duplicates) and a target integer target. Your task is to identify all distinct subsets of numbers from the candidates array such that the elements in each subset add up to exactly the target value.
Each element in the candidates array may only be used at most once within any single subset. Furthermore, the collection of subsets you return must not contain any duplicate subsets—two subsets are considered identical if they contain the same elements with the same frequencies, regardless of the order in which elements appear.
Return the list of all such unique subsets. The subsets can be returned in any order, and the elements within each subset can also be in any order.
candidates = [10,1,2,7,6,1,5]
target = 8[[1,1,6],[1,2,5],[1,7],[2,6]]There are exactly four unique subsets that sum to 8: [1,1,6] uses both 1s along with 6; [1,2,5] uses one of the 1s with 2 and 5; [1,7] pairs a 1 with 7; and [2,6] combines 2 and 6. Note that each candidate element is used at most once per subset, and no duplicate subsets exist in the output.
candidates = [2,5,2,1,2]
target = 5[[1,2,2],[5]]Two unique subsets sum to 5: [1,2,2] combines 1 with two of the three available 2s, and [5] uses the single 5. Although there are three 2s in the input, each subset uses candidates only once, and we avoid listing [1,2,2] multiple times despite multiple ways to choose two 2s.
candidates = [1,2,3,4,5]
target = 9[[1,3,5],[2,3,4],[4,5]]With no duplicate candidates, finding unique subsets is straightforward: [1,3,5], [2,3,4], and [4,5] each sum to exactly 9. Each element is distinct and can only be used once.
Constraints