forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsetSum.cpp
More file actions
30 lines (29 loc) · 689 Bytes
/
subsetSum.cpp
File metadata and controls
30 lines (29 loc) · 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
bool isSubsetSum(int set[], int n, int sum)
{
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;
if (set[n-1] > sum)
return isSubsetSum(set, n-1, sum);
return isSubsetSum(set, n-1, sum) ||
isSubsetSum(set, n-1, sum-set[n-1]);
}
int main()
{
int set[10000],n,sum;
//enter the size of set
cin>>n;
//enter the elements of set
for(int i=0;i<n;i++)
cin>>set[i];
//enter the required sum
cin>>sum;
if (isSubsetSum(set, n, sum) == true)
cout<<"Found a subset with given sum";
else
cout<<"No subset with given sum";
return 0;
}