subset sum problem using backtracking in c 2b 2b

Solutions on MaxInterview for subset sum problem using backtracking in c 2b 2b by the best coders in the world

showing results for - "subset sum problem using backtracking in c 2b 2b"
Giuseppe
21 Aug 2018
1    /* Part of Cosmos by OpenGenus Foundation */
2    #include<iostream>
3    using namespace std;
4    /*
5    *Find whether or not there exists any subset 
6    *  of array  that sum up to targetSum
7    */
8    class Subset_Sum
9    {
10        public:
11        // BACKTRACKING ALGORITHM
12        void subsetsum_Backtracking(int Set[] , int pos, int sum, int tmpsum, int size, bool & found)
13        {
14            if (sum == tmpsum)
15                found = true;
16                // generate nodes along the breadth
17            for (int i = pos; i < size; i++)
18            {
19             if (tmpsum + Set[i] <= sum)
20               {
21                  tmpsum += Set[i];   
22                  // consider next level node (along depth)
23                  subsetsum_Backtracking(Set, i + 1, sum, tmpsum, size, found);
24                  tmpsum -= Set[i];
25                }
26            }
27        }
28    };
29    
30    int main()
31    {
32        int i, n, sum;
33        Subset_Sum S;
34        cout << "Enter the number of elements in the set" << endl;
35        cin >> n;
36        int a[n];
37        cout << "Enter the values" << endl;
38        for(i=0;i<n;i++)
39          cin>>a[i];
40        cout << "Enter the value of sum" << endl;
41        cin >> sum;
42        bool f = false;
43        S.subsetsum_Backtracking(a, 0, sum, 0, n, f);
44        if (f)
45           cout << "subset with the given sum found" << endl;
46        else
47           cout << "no required subset found" << endl;   
48        return 0;
49    }
50
Wyatt
20 Nov 2018
1def SubsetSum(set, n, sum) :
2   # Base Cases
3   if (sum == 0) :
4      return True
5   if (n == 0 and sum != 0) :
6      return False
7   # ignore if last element is > sum
8   if (set[n - 1] > sum) :
9      return SubsetSum(set, n - 1, sum);
10   # else,we check the sum
11   # (1) including the last element
12   # (2) excluding the last element
13   return SubsetSum(set, n-1, sum) or SubsetSum(set, n-1, sumset[n-1])
14# main
15set = [2, 14, 6, 22, 4, 8]
16sum = 10
17n = len(set)
18if (SubsetSum(set, n, sum) == True) :
19   print("Found a subset with given sum")
20else :
21   print("No subset with given sum")
queries leading to this page
sum of subset problem using backtrackingsubset sum problem python backtrackingsubset sum problem without dpsubset sum problem dpbacktracking sum of subsetssubset sum problem using backtracking approach subset sum 7c backtracking 4sum of subset problem using backtracking time complexityalgorithm for sum of subsets backtrackingsubset sum problem backtracking javasubset sum problem backtracking in csubset sum problem hackerranksum of subsets with backtracking methodwrite the algorithm for sum of subset problem using backtracking sum of subset problem using backtracking algorithmsubset sum problem using backtracking pythonsum of subset problem using backtracking programming in c 2b 2bsubset sum problem backtracking codesum of subset problem using backtracking space complexityanalysis of backtracking solution for sum of subset problemsum of subsets using backtracking time complexitynumber of subset sum problem dppartition equal subset sum using backtrackingbacktracking can solve subset sum problemsum of subset problem using backtracking algorithm 5dbacktracking subset sumsum of subset problem using backtracking javasum of subsets using backtracking program i njavasubset sum backtracking c 2b 2bsubset sum problem backtracking using arraylistalgorithm for sum of subset problem using backtrackingrecursive backtracking algorithm for sum of subsetssum of subset problem using backtracking codesum of subsets backtracking in csum of subsets using backtracking techniquessum of subset problem using backtracking pythonsum of subsets complexity by backtrackingsubset sum problem using backtracking algorithm in javasubset sum problem backtrackingsum of subset 2c backtracking algorithmsum of subsets backtrackingwrite the algorithm for sum of subset problem using backtracking techniquesum of subset problem using backtracking in csum of subsets by backtrackingsum of subset problem 28using backtracking 29sum of subsets using backtracking optimal solutionsum of subset problem using backtracking time vs greedysubset sum problemsubset sum and backtacking algorithm pythonsum of subset problem using backtracking in c 2b 2bsum of subset backtraking pythonsum of subset problem using backtracking in python explainedall possible subsets whose sum equal to given n using backtrackingsubset sum problem using backtrackingsubset sum dp problemsum of subsets using backtracking in pythonsubset sum backtrackigsum of subsets using backtracking c 2b 2bsubset sum backtrackingsum of subset problem using backtracking pptbacktracking solution of the sum of subsets problemsum of subset problem backtracking algorithmsubset sum problem backtracking python codewrite the algorithm for sum of subsets problem using backtracking technique subset sum using backtrackingsum of subset problem backtracking analysissum of subsets using backtracking programsum of subset problem backtracking vs dynamic programmingsum of subset problem using backtracking constraintsum of subsets using backtracking algorithm for sum of subsets problem using backtrackingbacktracking algorithm subset sumsum of subset problem backtrackingbacktracking subset sum problemsubset sum python backtracksubset sum backtracking pythonsum of subset using backtrackingsubset sum problem using backtracking in c 2b 2bsum of subset problem using backtracking in pythonsubset sum problem solved by backtrackingsum of subsets backtracking algorithmsum of subset problem using backtracking complexitysum of subset problem backtrackingsubset sum problem using backtracking in c 2b 2b