Question #208209

K Sum Unique Combinations


Given a list of integers, write a program to print the count of all possible unique combinations of numbers whose sum is equal to K.


Input


The first line of input will contain space-separated integers. The second line of input will contain an integer, denoting K.


Output


The output should be containing the count of all unique combinations of numbers whose sum is equal to K.


Expert's answer

from itertools import combinations
numbers = list(map(int, input().split()))
k = int(input())
result = 0 
for size in range (1, len(numbers)):
    all_comb = [comb  for comb in combinations(numbers, size) if sum(comb) == k]
    uniq_comb =[]
    for comb in all_comb:
        for uniq in uniq_comb:
            if not(set(comb) ^ set(uniq)):
                break
        else:
            uniq_comb.append(comb)
    result += len(uniq_comb)
print(result)    
LATEST TUTORIALS
APPROVED BY CLIENTS