-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOTD-Partition Equal Subset Sum.java
58 lines (45 loc) · 1.57 KB
/
POTD-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
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
int N = Integer.parseInt(in.readLine());
String input_line[] = in.readLine().trim().split("\\s+");
int arr[] = new int[N];
for(int i = 0;i < N;i++)
arr[i] = Integer.parseInt(input_line[i]);
Solution ob = new Solution();
int x = ob.equalPartition(N, arr);
if(x == 1)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
// } Driver Code Ends
class Solution{
static boolean check(int i,int sm,long target,int[] arr,int N,Boolean[][] dp)
{
if(sm==target) return true;
if(i==N) return false;
if(dp[i][sm]!=null) return dp[i][sm];
if(sm+arr[i]>target)
return dp[i][sm]=check(i+1,sm,target,arr,N,dp);
return dp[i][sm]=check(i+1,sm,target,arr,N,dp) || check(i+1,sm+arr[i],target,arr,N,dp);
}
static int equalPartition(int N, int arr[])
{
long sum=0;
for(int i=0;i<N;i++) sum+=arr[i];
if(sum%2==1) return 0;
long half=sum/2;
Boolean[][] dp=new Boolean[N][(int)(sum)];
return check(0,0,half,arr,N,dp)?1:0;
}
}