You are given an array of distinct positive integers and a target value target. Your task is to find all unique combinations of elements whose sum equals the target.
You may use the same element multiple times. Return all valid combinations. The order of elements inside a combination does not matter.
Example 1
Input: arr[] = [2], target = 4
Output: [2 2]
Explanation: The element 2 can be used twice to reach the target.
Example 2
Input: arr[] = [2 3 6 7], target = 7
Output: [2 2 3] [7]
Explanation: Different combinations of the numbers add up to 7.
Example 3
Input: arr[] = [2 3 5], target = 8
Output: [2 2 2 2] [2 3 3] [3 5]
Explanation: Multiple selections of the given numbers form different sums equal to 8.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
2 3 6 7 7
7 223
2 3 5 8
35 2222 233