Count number of subsets with sum equal to k
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
To solve the problem of counting the number of subsets of a given set that sum up to a specified value, we delve into an area of combinatorial mathematics and dynamic programming. This is a classic problem often encountered in computer science and mathematics, known as the "Subset Sum Problem". Let's explore the technical details and examples that illustrate how to resolve this problem efficiently.
Problem Explanation
Given a set of integers, you need to find the total number of subsets whose elements add up to a specific sum, k
. Formally, you have a set of integers and a target sum k
. Your objective is to determine the number of subsets, , such that the sum of the elements in is equal to k
.
Dynamic Programming Approach
The dynamic programming (DP) technique is efficient for solving this problem due to its optimization by storing and reusing previously computed results. We utilize a two-dimensional DP table dp[i][j]
where i
represents the number of elements considered from the set, and j
represents the target sum.
Steps
- Initialization: • DP Table: Create a DP table of size
(n+1) x (k+1)initialized with zeros. Here,nis the number of elements in the set, andkis the target sum. • Base case: Setdp[0][0] = 1, indicating there's one way to make the sum zero using an empty subset. - Filling the Table: • Iterate through each element in the set and each possible sum. For each element
s_i: • If an element is not included, the number of subsets remainsdp[i-1][j]. • If an element is included, add the number of ways to formj - s_iusing the firsti-1elements:dp[i-1][j-s_i]. • Therefore,dp[i][j] = dp[i-1][j] + dp[i-1][j-s_i]. - Result Extraction: • After populating the DP table, the value
dp[n][k]contains the total number of subsets whose sum equalsk.
Technical Example
Let's demonstrate this approach using a small example:
• Set:
• Target Sum: k = 4
- DP Table Construction:
| i/j | 0 | 1 | 2 | 3 | 4 |
| 0 | 1 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 0 | 0 | 0 |
| 2 | 1 | 1 | 1 | 1 | 0 |
| 3 | 1 | 1 | 1 | 2 | 1 |
- Detailed Calculation: • Start with
dp[0][0]=1as there is one way to make the sum 0 (by choosing the empty subset). • Fori=1, : • Forj=1,
• Continue similarly until all entries are evaluated.
The final value dp[3][4] = 1
indicates there's one subset of that sums to 4 (\{1, 3\}
).
Key Considerations and Limitations
• Time Complexity: , where n
is the number of elements and k
is the target sum. This is an efficient computation compared to the brute-force approach.
• Space Complexity: due to the use of the DP table.
• Negative Numbers: The above approach assumes all numbers are non-negative. Special handling or transformations are required for sets containing negative numbers.
Summary Table
| Aspect | Detail |
| Method | Dynamic Programming |
| Time Complexity | |
| Space Complexity | |
| Initialization | dp[0][0] = 1 ; Other entries as 0 |
| Iterative Formula | dp[i][j] = dp[i-1][j] + dp[i-1][j-s_i] if , else dp[i-1][j] |
| --- | --- |
| Negative Numbers | Additional considerations needed |
Additional Considerations
• Use Cases: This technique is applicable in scenarios such as financial calculations, resource allocation, and other combinatorial optimization problems. • Implementation: While the DP solution is straightforward to implement, it is essential to carefully handle edge cases, such as empty sets or zero targets.
This exploration provides a comprehensive overview of how one can approach and solve the problem of counting subsets with a given sum using an efficient, methodical approach.
Related reading
- Count number of times each bit is set in a range of integers
- Count Number of Triples in an array that are collinear
- Count of co-prime pairs from two arrays in less than On2 complexity
- Count points in a rectangle
- Count points inside triangle fast
- Count sum of multiples of a number below N with O1 complexity?
- count the number of distinct absolute values among the elements of the array
- Count the number of Ks between 0 and N

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.