Python program to get all subsets having sum x Last Updated : 25 Feb, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list of n numbers and a number x, the task is to write a python program to find out all possible subsets of the list such that their sum is x. Examples: Input: arr = [2, 4, 5, 9], x = 15 Output: [2, 4, 9] 15 can be obtained by adding 2, 4 and 9 from the given list. Input : arr = [10, 20, 25, 50, 70, 90], x = 80 Output : [10, 70] [10, 20, 50] 80 can be obtained by adding 10 and 70 or by adding 10, 20 and 50 from the given list. Approach #1: It is a Brute Force approach. Find all possible subset sums of the given list and check if the sum is equal to x. The time complexity using this approach would be O(2^n) which is quite large. Python3 # Python code with time complexity # O(2^n)to print all subsets whose # sum is equal to a given value from itertools import combinations def subsetSum(n, arr, x): # Iterating through all possible # subsets of arr from lengths 0 to n: for i in range(n+1): for subset in combinations(arr, i): # printing the subset if its sum is x: if sum(subset) == x: print(list(subset)) # Driver Code: n = 6 arr = [10, 20, 25, 50, 70, 90] x = 80 subsetSum(n, arr, x) Output: [10, 70] [10, 20, 50] Approach #2: Meet in the middle is a technique that divides the search space into two equal-sized parts, performs a separate search on both the parts and then combines the search results. Using this technique, the two searches may require less time than one large search and turn the time complexity from O(2^n) to O(2^(n/2)). Python3 # Efficient Python code to # print all subsets whose sum # is equal to a given value from itertools import combinations def subsetSum(li, comb, sums): # Iterating through all subsets of # list li from length 0 to length of li: for i in range(len(li)+1): for subset in combinations(li, i): # Storing all the subsets in list comb: comb.append(list(subset)) # Storing the subset sums in list sums: sums.append(sum(subset)) def calcSubsets(n, arr, x): # Dividing the list arr into two lists # arr1 and arr2 of about equal sizes # by slicing list arr about index n//2: arr1, arr2 = arr[:n//2], arr[n//2:] # Creating empty lists comb1 and sums1 # to run the subsetSum function and # store subsets of arr1 in comb1 # and the subset sums in sums1: comb1, sums1 = [], [] subsetSum(arr1, comb1, sums1) # Creating empty lists comb2 and sums2 # to run the subsetSum function and # store subsets of arr2 in comb2 # and the subset sums in sums2: comb2, sums2 = [], [] subsetSum(arr2, comb2, sums2) # Iterating i through the indices of sums1: for i in range(len(sums1)): # Iterating j through the indices of sums2: for j in range(len(sums2)): # If two elements (one from sums1 # and one from sums2) add up to x, # the combined list of elements from # corresponding subsets at index i in comb1 # and j in comb2 gives us the required answer: if sums1[i] + sums2[j] == x: print(comb1[i] + comb2[j]) # Driver Code: n = 6 arr = [10, 20, 25, 50, 70, 90] x = 80 calcSubsets(n, arr, x) Output: [10, 70] [10, 20, 50] Comment More infoAdvertise with us Next Article Python Program to Find Sum of Array P prakharguptadpskuwait Follow Improve Article Tags : Python Python collections-module Python list-programs Practice Tags : python Similar Reads Python Program to Find Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid 4 min read Python Program for Subset Sum Problem | DP-25 Write a Python program for a given set of non-negative integers and a value sum, the task is to check if there is a subset of the given set whose sum is equal to the given sum. Examples: Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9Output: TrueExplanation: There is a subset (4, 5) with sum 9. Input: 7 min read Python | Find the number of unique subsets with given sum in array Given an array and a sum, find the count of unique subsets with each subset's sum equal to the given sum value. Examples: Input : 4 12 5 9 12 9 Output : 2 (subsets will be [4, 5] and [9]) Input : 1 2 3 4 5 10 Output : 3 We will use dynamic programming to solve this problem and this solution has time 2 min read SymPy | Subset.iterate_binary() in Python Subset.iterate_binary() : iterate_binary() is a sympy Python library function that iterates over the binary subsets by k steps. Value of 'k' can be positive or negative. Syntax : sympy.combinatorics.subset.Subset.iterate_binary() Return : iterates over the binary subsets by k steps Code #1 : iterate 1 min read SymPy | Subset.cardinality() in Python Subset.cardinality() : cardinality() is a sympy Python library function that returns the number of all possible subsets. Syntax : sympy.combinatorics.subset.Subset.cardinality() Return : number of all possible subsets Code #1 : cardinality() Example Python3 1=1 # Python code explaining # SymPy.Subse 1 min read SymPy | Subset.subset_from_bitlist() in Python Subset.subset_from_bitlist() : subset_from_bitlist() is a sympy Python library function that returns the subset defined by the bitlist. Syntax : sympy.combinatorics.subset.Subset.subset_from_bitlist() Return : the subset defined by the bitlist Code #1 : subset_from_bitlist() Example Python3 1=1 # Py 1 min read Like