Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- bool recursivecheck(int curr, int end, vector<int>& v, int target, int &tempsum)
- {
- //base case
- if (tempsum == target) return true;
- if (curr == end) return false;
- //let's include this
- tempsum += v[curr];
- if (recursivecheck(curr + 1, end, v, target, tempsum) == true) return true;
- //let's exclude this
- tempsum -= v[curr];
- if (recursivecheck(curr + 1, end, v, target, tempsum) == true) return true;
- return false;
- }
- int main()
- {
- int n, target; cin >> n >> target;
- vector<int> v(n);
- for (int i = 0; i < n; i++) cin >> v[i];
- int tempsum = 0;
- if (recursivecheck(0, n, v, target, tempsum))
- {
- cout << "Found the subset" << endl;
- }
- else cout << "Not Found The subset" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement