-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanSum.cpp
More file actions
68 lines (68 loc) · 1.52 KB
/
canSum.cpp
File metadata and controls
68 lines (68 loc) · 1.52 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <map>
using namespace std;
// Time Complexity is O(nm) And Space Complexity is O(nm).
bool canSum(int *ar,int n,int sum,map<int,bool> &mp){
if (sum==0){
return true;
}
if (sum<0){
return false;
}
if (mp.count(sum)!=0){
return mp[sum];
}
for (int i = 0; i < n; ++i) {
int rem = sum-ar[i];
if (canSum(ar,n,rem,mp)){
mp[sum] = true;
return mp[sum];
}
}
mp[sum] = false;
return mp[sum];
}
// Time Complexity is O(nm) And Space Complexity is O(nm).
bool canSum2(const int *ar,int n,int sum){
int *dp = new int[sum+1];
for (int i = 0; i <=sum; ++i) {
dp[i] = false;
}
dp[0] = true;
for (int i = 1; i <=sum; ++i) {
if (dp[i]!= true) {
for (int j = 0; j < n; ++j) {
if (i >= ar[j]) {
dp[i] = dp[i - ar[j]];
}
}
}
}
return dp[sum];
}
int main(){
cout<<"Enter The value of n:\n";
int n;
cin>>n;
cout<<"Enter The value of array:\n";
int *ar = new int[n];
for (int i = 0; i < n; ++i) {
cin>>ar[i];
}
cout<<"Enter The value of sum:\n";
int sum;
cin>>sum;
map<int,bool> mp;
cout<<"Using Top Down Approach:\n";
if (canSum(ar,n,sum,mp)) {
cout << "YES\n";
} else{
cout<<"NO\n";
}
cout<<"Using Bottom Up Approach:\n";
if (canSum2(ar,n,sum)) {
cout << "YES\n";
} else{
cout<<"NO\n";
}
}