Coding Challenges < Python >

What will be the output of this code?

def count_subsets(nums, target):
    memo = {}

    def backtrack(index, remaining):
        if (index, remaining) in memo:
            return memo[(index, remaining)]
        
        if remaining == 0:
            return 1
        if remaining < 0 or index == len(nums):
            return 0
        
        count = backtrack(index + 1, remaining - nums[index]) + backtrack(index + 1, remaining)
        memo[(index, remaining)] = count
        return count

    return backtrack(0, target)

nums = [1, 2, 3, 4, 5]
target = 7
print(count_subsets(nums, target))
Solution
No , it’s 3 😌
Was this page helpful?