Problem Statement:


Given an array of distinct integers and a target integer, return a list of all unique combinations of integers from the given array where the chosen integers sum to target.
The same number may be chosen from the array an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

Example:
Input = [2,3,5], target = 5
Output: [[3, 2],[5]]

Input = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Input = [1], target = 1
Output: [[1]]

Input = [1], target = 2
Output: [[1,1]]

Solution:


  • NOTE: I highly recommend going through the Backtracking chapters in the order they are given in the Index page to get the most out of it and be able to build a rock-solid understanding.


Algorithm:



Login to Access Content




Java Code:



Login to Access Content



Python Code:



Login to Access Content




Don't forget to take in-depth look at the other backtracking problems because that is what would make you comfortable with using the backtracking template and master the art of Backtracking:


Instructor:



If you have any feedback, please use this form: https://thealgorists.com/Feedback.



Help Your Friends save 40% on our products

wave