The Wayback Machine - https://web.archive.org/web/20240811124051/https://www.geeksforgeeks.org/csharp-program-for-subset-sum-problem-dp-25/
Open In App

C# Program for Subset Sum Problem | DP-25

Last Updated : 10 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Write a C# 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 = 9
Output: True
Explanation: There is a subset (4, 5) with sum 9.

Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 30
Output: False
Explanation: There is no subset that adds up to 30.

C# Program for Subset Sum Problem using Recursion:

For the recursive approach, there will be two cases.

  • Consider the ‘last’ element to be a part of the subset. Now the new required sum = required sum – value of ‘last’ element.
  • Don’t include the ‘last’ element in the subset. Then the new required sum = old required sum.

In both cases, the number of available elements decreases by 1.

Step-by-step approach:

  • Build a recursive function and pass the index to be considered (here gradually moving from the last end) and the remaining sum amount.
  • For each index check the base cases and utilize the above recursive call.
  • If the answer is true for any recursion call, then there exists such a subset. Otherwise, no such subset exists.

Below is the implementation of the above approach.

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)
            return false;
 
        // If last element is greater than sum,
        // then ignore it
        if (set[n - 1] > sum)
            return isSubsetSum(set, n - 1, sum);
 
        // Else, check if sum can be obtained
        // by any of the following
        // (a) including the last element
        // (b) excluding the last element
        return isSubsetSum(set, n - 1, sum)
            || isSubsetSum(set, n - 1, sum - set[n - 1]);
    }
 
    // Driver code
    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


Output

Found a subset with given sum

Time Complexity: O(2n)
Auxiliary space: O(n)

C# Program for Subset Sum Problem using Memoization:

As seen in the previous recursion method, each state of the solution can be uniquely identified using two variables – the index and the remaining sum. So create a 2D array to store the value of each state to avoid recalculation of the same state.

Below is the implementation of the above approach:

C#




// C# program for the above approach
using System;
 
class GFG
{
    // Check if possible subset with
    // given sum is possible or not
    static int subsetSum(int []a, int n, int sum)
    {    
        // Storing the value -1 to the matrix
        int [,]tab = new int[n + 1,sum + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= sum; j++) {
                tab[i,j] = -1;
            }
        }
 
        // If the sum is zero it means
        // we got our expected sum
        if (sum == 0)
            return 1;
 
        if (n <= 0)
            return 0;
 
        // If the value is not -1 it means it
        // already call the function
        // with the same value.
        // it will save our from the repetition.
        if (tab[n - 1,sum] != -1)
            return tab[n - 1,sum];
 
        // If the value of a[n-1] is
        // greater than the sum.
        // we call for the next value
        if (a[n - 1] > sum)
            return tab[n - 1,sum]
                = subsetSum(a, n - 1, sum);
        else {
 
            // Here we do two calls because we
            // don't know which value is
            // full-fill our criteria
            // that's why we doing two calls
            if (subsetSum(a, n - 1, sum) != 0
                || subsetSum(a, n - 1, sum - a[n - 1])
                    != 0) {
                return tab[n - 1,sum] = 1;
            }
            else
                return tab[n - 1,sum] = 0;
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 5;
        int []a = { 1, 5, 3, 7, 4 };
        int sum = 12;
 
        if (subsetSum(a, n, sum) != 0) {
            Console.Write("YES\n");
        }
        else
            Console.Write("NO\n");
    }
}
 
// This code is contributed by shivanisinghss2110


Output

YES

Time Complexity: O(sum*n)
Auxiliary space: O(n)

C# Program for Subset Sum Problem using Dynamic Programming:

We can solve the problem in Pseudo-polynomial time we can use the Dynamic programming approach.

So we will create a 2D array of size (n + 1) * (sum + 1) of type boolean. The state dp[i][j] will be true if there exists a subset of elements from set[0 . . . i] with sum value = ‘j’.

The dynamic programming relation is as follows:

if (A[i-1] > j)
dp[i][j] = dp[i-1][j]
else
dp[i][j] = dp[i-1][j] OR dp[i-1][j-set[i-1]]

Below is the implementation of the above approach:

C#




// A Dynamic Programming 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)
    {
        // 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];
 
        // If sum is 0, then answer is true
        for (int i = 0; i <= n; i++)
            subset[0, i] = true;
 
        // If sum is not 0 and set is empty,
        // then answer is false
        for (int i = 1; i <= sum; i++)
            subset[i, 0] = false;
 
        // Fill the subset table in bottom up manner
        for (int i = 1; i <= sum; i++) {
            for (int j = 1; j <= n; j++) {
                subset[i, j] = subset[i, j - 1];
                if (i >= set[j - 1])
                    subset[i, j]
                        = subset[i, j]
                        || subset[i - set[j - 1], j - 1];
            }
        }
 
        return subset[sum, n];
    }
 
    // Driver code
    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


Output

Found a subset with given sum

Time Complexity: O(sum * n), where n is the size of the array.
Auxiliary Space: O(sum*n), as the size of the 2-D array is sum*n.

C# Program for Subset Sum Problem using Dynamic Programming with space optimization to linear:

In previous approach of dynamic programming we have derive the relation between states as given below:
if (A[i-1] > j)
dp[i][j] = dp[i-1][j]
else
dp[i][j] = dp[i-1][j] OR dp[i-1][j-set[i-1]]
If we observe that for calculating current dp[i][j] state we only need previous row dp[i-1][j] or dp[i-1][j-set[i-1]].
There is no need to store all the previous states just one previous state is used to compute result.

Step-by-step approach:

  • Define two arrays prev and curr of size Sum+1 to store the just previous row result and current row result respectively.
  • Once curr array is calculated then curr becomes our prev for the next row.
  • When all rows are processed the answer is stored in prev array.

Below is the implementation of the above approach:

C#




using System;
 
class Program
{
    // Returns true if there is a subset of set[]
    // with sum equal to given sum
    static bool IsSubsetSum(int[] set, int n, int sum)
    {
        // Create a 2D array 'dp' with dimensions (n+1) x (sum+1)
        bool[,] dp = new bool[n + 1, sum + 1];
 
        // If sum is 0, then answer is true
        for (int i = 0; i <= n; i++)
            dp[i, 0] = true;
 
        // If sum is not 0 and set is empty,
        // then answer is false
        for (int i = 1; i <= sum; i++)
            dp[0, i] = false;
 
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= sum; j++)
            {
                if (j < set[i - 1])
                    dp[i, j] = dp[i - 1, j];
                if (j >= set[i - 1])
                    dp[i, j] = dp[i - 1, j] || dp[i - 1, j - set[i - 1]];
            }
        }
 
        return dp[n, sum];
    }
 
    static void Main()
    {
        int[] set = { 3, 34, 4, 12, 5, 2 };
        int sum = 9;
        int n = set.Length;
 
        if (IsSubsetSum(set, n, sum))
            Console.WriteLine("Found a subset with given sum");
        else
            Console.WriteLine("No subset with given sum");
    }
}


Output

Found a subset with given sum

Time Complexity: O(sum * n), where n is the size of the array.
Auxiliary Space: O(sum), as the size of the 1-D array is sum+1.

Please refer complete article on Subset Sum Problem | DP-25 for more details!



Previous Article
Next Article

Similar Reads

C# Program for Activity Selection Problem | Greedy Algo-1
You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. Example: Example 1 : Consider the following 3 activities sorted by finish time. start[] = {10, 12, 20}; finish[] = {20, 25, 30}; A person
2 min read
C# Program to Calculate the Sum of Array Elements using the LINQ Aggregate() Method
Given an array of integers, now we calculate the sum of array elements. So we use the Aggregate() method of LINQ. This method applies a function to all the elements of the source sequence and calculates a cumulative result and return value. This method is overloaded in three different ways: Aggregate&lt;TSource, TAccumulate, TResult&gt;(IEnumerable
2 min read
C# Program for Maximum sum rectangle in a 2D matrix | DP-27
Write a C# program for a given 2D array, the task is to find the maximum sum subarray in it. For example, in the following 2D array, the maximum sum subarray is highlighted with blue rectangle and sum of this subarray is 29. This problem is mainly an extension of the Largest Sum Contiguous Subarray for 1D array. Recommended: Please solve it on “PRA
6 min read
C# Program for Largest Sum Contiguous Subarray
Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Kadane's Algorithm: Initialize: max_so_far = INT_MIN max_ending_here = 0 Loop for each element of the array (a) max_ending_here = m
5 min read
C# Program for Program for array rotation
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make array Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. METHOD 1 (Using temp array) Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store the first d elements in a temp array temp[] = [1,
5 min read
C# Program to Demonstrate the Example of LINQ Union() Method with StringComparer
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives a feature to .NET languages to create queries to retrieve data from the data source. Here in this article, we will demonstrate the example of the LINQ Union() method with the StringComparer. 1. Union() Method: This method is used to get the unique elements from th
2 min read
C# Program to Demonstrate the Example of an Array of Delegates
In C#, a delegate is an object which refers to a method or it is a reference type variable that can hold a reference to the methods. Delegates are similar to the C/C++ function pointer. It also provides a way that tells which method is to be called when an event is triggered. As the name suggests an array of delegates means declaring an array of de
3 min read
C# Program to Check Given Directory Exists or not
Given a directory, now our task is to check given directory exists or not. So to this task, we use the Exists() method of the Directory class. This method will return true if the given directory exists, otherwise false. Syntax: public static bool Exists (string? Mypath); Where, Mypath is a parameter of Exists() method of string type. It represents
2 min read
C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7
Given a graph and a source vertex in the graph, find shortest paths from source to all vertices in the given graph. Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like Prim's MST, we generate a SPT (shortest path tree) with given source as root. We maintain two sets, one set contains vertices included in shortes
5 min read
C# Program for Longest Palindromic Subsequence | DP-12
Given a sequence, find the length of the longest palindromic subsequence in it. As another example, if the given sequence is "BBABCBCAB", then the output should be 7 as "BABCBAB" is the longest palindromic subsequence in it. "BBBBB" and "BBCBB" are also palindromic subsequences of the given sequence, but not the longest ones. 1) Optimal Substructur
4 min read
Article Tags :
three90RightbarBannerImg