Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int IsSubsetSum(int arr[],int n,int sum){
- if(sum==0)
- return 1;
- if(n==0&& sum!=0)
- return 0;
- if(arr[n-1]>sum)
- return IsSubsetSum(arr,n-1,sum);
- return IsSubsetSum(arr,n-1,sum-arr[n-1])|| IsSubsetSum(arr,n-1,sum);
- }
- int main(){
- int arr[] = { 3, 34, 4, 12, 5, 2 };
- int sum = 30;
- int n = 6;
- if (IsSubsetSum(arr,n,sum))
- cout<<("Found a subset with given sum");
- else
- cout<<("No subset with given sum");
- return 0;
- }
Add Comment
Please, Sign In to add comment