Suppose we have an array called nums and another value sum. We have to check whether it is possible to get sum by adding elements present in nums, we may pick a single element multiple times.
So, if the input is like nums = [2, 3, 5] sum = 28, then the output will be True as we can get 26 by using 5 + 5 + 5 + 5 + 3 + 3 + 2
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
MAX = 1000 table =  * MAX def util(nums): table = 1 nums.sort() for i in range(len(nums)): val = nums[i] if table[val]: continue for j in range(MAX - val): if table[j]: table[j + val] = 1 def solve(nums, sum): util(nums) if table[sum]: return True return False nums = [2, 3, 5] sum = 28 print (solve(nums, sum))
[2, 3, 5], 28