0% completed
Problem Statement
Given a set of positive numbers, determine if there exists a subset whose sum is equal to a given number 'S'.
Example 1:
Input: {1, 2, 3, 7}, S=6
Output: True
The given set has a subset whose sum is '6': {1, 2, 3}
Example 2:
Input: {1, 2, 7, 1, 5}, S=10
Output: True
The given set has a subset whose sum is '10': {1, 2, 7}
Example 3:
Input: {1, 3, 4, 8}, S=6
Output: False
The given set does not have any subset whose sum is equal to '6'.
Basic Solution
This problem follows the 0/1 Knapsack pattern and is quite similar to "Equal Subset Sum Partition"
.
A basic brute-force solution could be to try all subsets of the given numbers to see if any set has a sum equal to 'S'.
So our brute-force algorithm will look like:
for each number 'i' create a new set which INCLUDES number 'i' if it does not exceed 'S', and recursively process the remaining numbers create a new set WITHOUT number 'i', and recursively process the remaining numbers return true if any of the above two sets has a sum equal to 'S', otherwise return false
Since this problem is quite similar to "Equal Subset Sum Partition"
, let's jump directly to the bottom-up dynamic programming solution.
Bottom-up Dynamic Programming
We'll try to find if we can make all possible sums with every subset to populate the array dp[TotalNumbers][S+1]
.
For every possible sum ‘s’ (where 0 <= s <= S), we have two options:
- Exclude the number. In this case, we will see if we can get the sum ‘s’ from the subset excluding this number =>
dp[index-1][s]
- Include the number if its value is not more than ‘s’. In this case, we will see if we can find a subset to get the remaining sum =>
dp[index-1][s-num[index]]
If either of the above two scenarios returns true, we can find a subset with a sum equal to ‘s’.
Let's draw this visually, with the example input {1, 2, 3, 7}, and start with our base case of size zero:


![sum: 1, index:1=> (dp[index-1][sum] , as the 'sum' is less than the number at index '1' (i.e., 1 < 2)](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A678f4fd-c43c-a62-407d-38616184d2e.png%3Fgeneration%3D1668726886273283%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 2, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A8eb51e8-674c-5b84-2721-0dabd178f82.png%3Fgeneration%3D1668726928390147%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 3, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A3f743fd-1b55-ebd1-4a86-4ae2a3bb0737.jpg%3Fgeneration%3D1669101075217463%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 4-6 index:1=> (dp[index-1][sum] || dp[index-1][sum-2])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3Ad10482a-647a-40b7-36a2-1e8b7885b208.jpg%3Fgeneration%3D1669101111416121%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 1,2,3, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A43cc84b-7f53-b0a-2083-aa7f3c4207.jpg%3Fgeneration%3D1669101140563196%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 4, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A3a3bc6e-bc77-03a0-aaa2-1113b3d82b0.jpg%3Fgeneration%3D1669101175869233%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 5, 6, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A4f4416d-3c6c-e305-47d-0a06566356de.jpg%3Fgeneration%3D1669101215304561%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
![sum: 1-6, index:3=> (dp[index-1][sum] || dp[index-1][sum-7])](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fdownload%2Fstorage%2Fv1%2Fb%2Fdesigngurus-prod.appspot.com%2Fo%2FdocImages%252F6375ea0bb6d31bf12a61119f%252Fimg%3A387c73a-21e-f64e-1f-b1af1a76fc45.jpg%3Fgeneration%3D1669101343746216%26alt%3Dmedia&w=3840&q=75&dpl=dpl_77Ae7vEwq3A8eA9La8V6vjrYv3GG)
Code
Here is the code for our bottom-up dynamic programming approach:
The above solution has time and space complexity of O(N*S), where ‘N’ represents total numbers and ‘S’ is the required sum.
Challenge
Can we further improve our bottom-up DP solution? Can you find an algorithm that has O(S) space complexity?
Hint
Similar to the space optimized solution for "0/1 Knapsack"
.
Try it yourself
Solution
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible