Subset Sum Problem
Subset Sum Problem
Given a set of non-negative integers, and a value sum, determine if there is a subset of the given
set with sum equal to given sum.
Example:
Let isSubSetSum(int set[], int n, int sum) be the function to find whether there is a subset of set[]
with sum equal to sum. n is the number of elements in set[].
Following is naive recursive implementation that simply follows the recursive structure
mentioned above.
div class="responsive-tabs">
C++
// A recursive solution for subset sum problem
#include <stdio.h>
// Returns true if there is a subset of set[] with sun equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
// Base Cases
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;
Java
// A recursive solution for subset sum
// problem
class GFG {
Python3
# A recursive solution for subset sum
# problem
# Base Cases
if (sum == 0) :
return True
if (n == 0 and sum != 0) :
return False
C#
// A recursive solution for subset sum problem
using System;
class GFG
{
// Returns true if there is a subset of set[] with sum
// equal to given sum
static bool isSubsetSum(int []set, int n, int sum)
{
// Base Cases
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;
// Driver program
public static void Main ()
{
int []set = {3, 34, 4, 12, 5, 2};
int sum = 9;
int n = set.Length;
if (isSubsetSum(set, n, sum) == true)
Console.WriteLine("Found a subset with given sum");
else
Console.WriteLine("No subset with given sum");
}
}
PHP
<?php
// A recursive solution for subset sum problem
// Driver Code
$set = array(3, 34, 4, 12, 5, 2);
$sum = 9;
$n = 6;
The above solution may try all subsets of given set in worst case. Therefore time complexity of
the above solution is exponential. The problem is in-fact NP-Complete (There is no known
polynomial time solution for this problem).
C++
// A Dynamic Programming solution for subset sum problem
#include <stdio.h>
// Returns true if there is a subset of set[] with sun equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
// The value of subset[i][j] will be true if there is a
// subset of set[0..j-1] with sum equal to i
bool subset[n+1][sum+1];
Java
// A Dynamic Programming solution for subset
// sum problem
class GFG {
return subset[sum][n];
}
Python3
# A Dynamic Programming solution for subset sum problem
# Returns true if there is a subset of
# set[] with sun equal to given sum
C#
// A Dynamic Programming solution for subset sum problem
using System;
class GFG
{
// Returns true if there is a subset
// of set[] with sun equal to given sum
static bool isSubsetSum(int []set, int n, int sum)
{
// The value of subset[i][j] will be true if there
// is a subset of set[0..j-1] with sum equal to i
bool [,]subset = new bool[sum+1,n+1];
}
}
return subset[sum,n];
}
// Driver program
public static void Main ()
{
int []set = {3, 34, 4, 12, 5, 2};
int sum = 9;
int n = set.Length;
if (isSubsetSum(set, n, sum) == true)
Console.WriteLine("Found a subset with given sum");
else
Console.WriteLine("No subset with given sum");
}
}
// This code is contributed by Sam007
PHP
<?php
// A Dynamic Programming solution for
// subset sum problem
return $subset[$n][$sum];
}
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above