Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

101. Combination Sum II

MediumDay 29 of 60Recursion & Backtracking

You are given an array candidates (which may contain repeated numbers) and an integer target. Return every unique combination of candidates whose sum equals target. Each element of the array may be used at most once, and the answer must not contain the same combination twice. Combinations may be returned in any order.

Example 1
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output: [[1,1,6],[1,2,5],[1,7],[2,6]]
The two 1s can be used together in [1,1,6], but [1,7] appears only once even though there are two 1s.
Example 2
Input: candidates = [2,5,2,1,2], target = 5
Output: [[1,2,2],[5]]
Constraints
  • 1 <= candidates.length <= 100
  • 1 <= candidates[i] <= 50
  • 1 <= target <= 30
Hints
Frequently asked at: Amazon, Meta, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min