-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp.cpp
107 lines (69 loc) · 1.99 KB
/
dp.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Question Given an array arr[] of size N, check if it can be partitioned into two parts such that the sum of //elements in both parts is the same.
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
int equalPartition(int N, int arr[])
{
int sum=0;
for(int i=0; i<N; i++){
if (sum+=arr[i]);
}
if (sum%2!=0){
return 0;
}
else
{
bool result[sum/2 + 1][N+1];
for (int i=0;i<=sum/2;i++)
result[i][0]=false;
for(int i=0; i<=N; i++){
result[0][i]=true;
}
for (int i=1 ;i<=sum/2; i++){
for (int j=1; j<=N; j++){
result[i][j]=result[i][j-1];
if(i>=arr[j-1])
result[i][j]=result[i][j] || result[i-arr[j-1]][j-1];
}
}
if (result[sum/2][N])
return 1;
else
return 0;
}
}
};
int main(){
int t;
cin>>t;
while(t--){
int N;
cin>>N;
int arr[N];
for(int i = 0;i < N;i++)
cin>>arr[i];
Solution ob;
if(ob.equalPartition(N, arr))
cout<<"YES\n";
else
cout<<"NO\n";
}
return 0;
}
//Given an integer array nums, return the length of the longest strictly increasing subsequence.
int lengthOfLIS(vector<int>& nums) {
vector<int> vec(nums.size(), 1);
int maxval=1;
for(int i=1;i<nums.size();i++){
for(int j=0;j<i;j++){
if (nums[j]<nums[i]){
vec[i]=max(vec[j]+1, vec[i]);
}
}
if (maxval<vec[i]){
maxval=vec[i];
}
}
return maxval;
}