Problem

With the target number as given array of integers (each element is differrent from each other). Find the best combination of the elements in the array to obtain the target value(Each value can be used more than 1 time).

Example:

                                
                                    #target = 8, nums = [5, 2, 3] 
result = bestSum(target, nums) # [5, 3]

Solution

There are many approaching way to solve this problem, but in this post, we use the tabular solution of this problem

Firstly, we start with the initial array. This array has the length of target + 1, whereas each index stores the best solution of this target number.

Fig 1: The inital array filled with empty array

With the preknown fact, if the target = 0, we can obtain the optimal solution for this problem: result = []