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:



This is a Premium content.
Please subscribe to the Algorithms course to access the detailed Algorithm discussion.





Java Code:



This is a Premium content.
Please subscribe to Algorithms course to access the code.




Python Code:



This is a Premium content.
Please subscribe to Algorithms course to access the code.





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.




Subscribe to Our Youtube Channel

Follow Us On LinkedIn
wave