111. Combination Sum II

Given collection of candidate numbers (candidates) and a integer target.Find all unique combinations in candidates where the sum is equal to the target.There can only be one usage of each number in the candidates combination and return the answer in sorted order.

e.g : The combination [1, 1, 2] and [1, 2, 1] are not unique.

Example 1:

Input : candidates = [2, 1, 2, 7, 6, 1, 5] , target = 8

Output : [ [1, 1, 6] , [1, 2, 5] , [1, 7] , [2, 6] ]

Explanation : The combinations sum up to target are

1 + 1 + 6 => 8.

1 + 2 + 5 => 8.

1 + 7 => 8.

2 + 6 => 8.

Example 2:

Input : candidates = [2, 5, 2, 1, 2] , target = 5

Output : [ [1, 2, 2] , [5] ]

Explanation : The combinations sum up to target are

1 + 2 + 2 => 5.

5 => 5.

Now Your Turn!

Pick the correct output for the given input

Input : candidates = [2, 1, 2] , target = 5

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int>& candidates, int target) {
//your code goes here
}
};
Test Case

Input:

Candidates
Target