Python | Find the number of unique subsets with given sum in array Last Updated : 17 Sep, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 complexity of O(n2). Below is dp[][] used in the code - _ | 4 12 5 9 12 0 |[1, 1, 1, 1, 1] 1 |[0, 0, 0, 0, 0] 2 |[0, 0, 0, 0, 0] 3 |[0, 0, 0, 0, 0] 4 |[1, 1, 1, 1, 1] 5 |[0, 0, 1, 1, 1] 6 |[0, 0, 0, 0, 0] 7 |[0, 0, 0, 0, 0] 8 |[0, 0, 0, 0, 0] 9 |[0, 0, 1, 2, 2] Below is the Python Code : Python3 1== # Python code to find the number of unique subsets # with given sum in the given array def help(a,s): dp = [[0 for i in range(len(a))] for j in range(0,s+1)] for i in range(len(a)): dp[0][i] = 1 for i in range(1,s+1): if i == a[0]: dp[i][0] = 1 for i in range(1, s+1): for j in range(1, len(a)): if a[j]<=i: dp[i][j] = dp[i][j-1] + dp[i-a[j]][j-1] else: dp[i][j] = dp[i][j-1] return dp[s][len(a)-1] # driver code a = [4, 12, 5, 9, 12] s = 9 print(help(a,s)) Output : 2 Comment More infoAdvertise with us Next Article Python | Minimum number of subsets with distinct elements using Counter P PreetAnmol Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python | Minimum number of subsets with distinct elements using Counter You are given an array of n-element. You have to make subsets from the array such that no subset contain duplicate elements. Find out minimum number of subset possible. Examples: Input : arr[] = {1, 2, 3, 4} Output :1 Explanation : A single subset can contains all values and all values are distinct 4 min read Given two arrays, find n+m-1 unique sum pairs Given 2 integer arrays A and B(with no repeated elements in each) of size N and M respectively, form N + M - 1 pairs, each pair having one element from both the arrays, such that the sum of all the pairs is unique. For each pair, print the index of the elements in their respective array. Example: In 3 min read Python | sympy.combinatorics.Subset().cardinality method With the help of sympy.combinatorics.Subset().cardinality method, we can find the total cardinality of subsets by using sympy.combinatorics.Subset().cardinality method. Syntax : sympy.combinatorics.Subset().cardinality Return : Return the total cardinality of subset. Example #1 : In this example we 1 min read Splitting Cells and Counting Unique Values in Python List Here, we need to split elements of a list and count the unique values from the resulting segments. For example, given a list like ["a,b,c", "b,c,d" , e,f"], we want to extract each value, count only the unique ones, and return the result. Letâs examine different methods to achieve this, starting fro 3 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 Program to print all distinct elements of a given integer array in Python | Ordered Dictionary Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted. Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45} Output: 12, 10, 9, 45, 2 Input: arr[] = {1, 2, 3, 4, 5} Out 2 min read Like