-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04 subset sum(knapsack variation).cpp
More file actions
52 lines (43 loc) · 1.12 KB
/
Copy path04 subset sum(knapsack variation).cpp
File metadata and controls
52 lines (43 loc) · 1.12 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
#include <iostream>
using namespace std;
// first we nake matric andc heck it out
bool subsetsum(int arr[],int n,int sum)
{
// dpmatric we will take it
bool t[n+1][sum+1];
// okay so before we will take the first row adn first column
for(int =0; i<=n; i++)
{
for(int = 0; j<=sum; j++)
{
if(i==0)
// when empty meanss hence than reture falsse no sum of array at that moment
t[i]t[j]=false;
if(j==0)
// when empty means hecnec than return true
t[i]t[j]=true;
}
}
}
// we are starting from one as i row is and colum is already conscidered
for(int i=1; i<=n; i++)
{
for(int j=1; j<=sum; j++)
{
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j];
else
t[i][j] = t[i - 1][j];
// if sum is less tha array siz
}
}
return t[n][sum]; //return T/F
}
int main() {
int n; cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int sum; cin >> sum;
isSubsetPossible(arr, n, sum) ? cout << "Yes\n" : cout << "No\n";
return 0;
}