-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003-partition-equal-subset-sum.java
70 lines (61 loc) · 1.7 KB
/
003-partition-equal-subset-sum.java
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
// Problem Link: https://www.geeksforgeeks.org/problems/subset-sum-problem2014/1
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
// User function Template for Java
class Solution{
static boolean findSubSetSumRecursive(int N, int arr[], int sum){
if(N==0){
return false;
}
if(sum == 0){
return true;
}
if(arr[N-1]<=sum){
return findSubSetSum(N-1, arr, sum-arr[N-1]) || findSubSetSum(N-1, arr, sum);
} else {
return findSubSetSum(N-1, arr, sum);
}
}
static boolean findSubSetSum(int N, int arr[], int sum){
boolean[][] t = new boolean[N+1][sum+1];
for(int i = 0;i<=N;i++){
for(int j=0;j<=sum;j++){
if(i==0){
t[i][j]=false;
}
if(j==0){
t[i][j]=true;
}
}
}
for(int i = 1;i<=N;i++){
for(int j=1;j<=sum;j++){
if(arr[i-1]<=j){
t[i][j] = t[i-1][j-arr[i-1]] || t[i-1][j];
}
else
t[i][j]= t[i-1][j];
}
}
return t[N][sum];
}
static int equalPartition(int N, int arr[])
{
// code here
int sum = 0;
for(int i = 0;i<N;i++){
sum+=arr[i];
}
if(sum%2!=0){
return 0;
}
// boolean bool = findSubSetSumRecursive(N, arr, sum/2);
boolean bool = findSubSetSum(N, arr, sum/2);
if(bool){
return 1;
} else
return 0;
}
}