-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path416_Partition_Equal_Subset_Sum_LeetCode.java
More file actions
56 lines (47 loc) · 1.13 KB
/
416_Partition_Equal_Subset_Sum_LeetCode.java
File metadata and controls
56 lines (47 loc) · 1.13 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
class Solution
{
public boolean subsetSum(int a[],int n,int sum)
{
boolean dp[][] = new boolean[n+1][sum+1];
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<sum;j++)
{
if(i==0)
dp[i][j] = false;
else
dp[i][j] = true;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=sum;j++)
{
if(a[i-1]>j)
{
dp[i][j] = dp[i-1][j];
}
else
{
dp[i][j] = dp[i-1][j-a[i-1]] || dp[i-1][j];
}
}
}
if(dp[n][sum] == true)
return true;
else
return false;
}
public boolean canPartition(int[] a)
{
int n = a.length;
int i,sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
if(sum%2!=0)
return false;
else
return (subsetSum(a,n,sum/2));
}
}