forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitionProblem.java
79 lines (74 loc) · 1.71 KB
/
partitionProblem.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
71
72
73
74
75
76
77
78
79
/*
given an array is it possible to split it up into 2 equal
sum partitions. Partition need not be equal sized. Just equal sum.
*/
import java.util.*;
class partitionProblem
{
public static void subsetSumPath_util(int[] arr, int sum)
{
boolean[][] T = new boolean[arr.length+1][sum+1];
// if sum is 0
for(int i=0;i<=arr.length;i++)
T[i][0] = true;
for(int i=1;i<=arr.length;i++)
{
for(int j=1;j<=sum;j++)
{
if(j < arr[i-1])
T[i][j] = T[i-1][j];
else
T[i][j] = T[i-1][j] || T[i-1][j-arr[i-1]];
}
}
//for(boolean[] temp : T)
// System.out.println(Arrays.toString(temp));
boolean[] output = new boolean[arr.length];
int m=arr.length;
int i=arr.length;
int j=sum;
while(j>0)
{
if(T[i][j] == T[i-1][j])
{
i--;
}
else
{
j = j - arr[i-1];
output[i-1] = true;
}
//System.out.println(arr[i]+" i:"+i+" j:"+j);
}
System.out.println("first subset");
for(i=0;i<arr.length;i++)
{
if(!output[i])
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("Second subset");
for(i=0;i<arr.length;i++)
{
if(output[i])
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args)
{
int[] arr = {2,3,7,8,10};
int sum = 0;
for(int i=0;i<arr.length;i++)
sum +=arr[i];
if((sum&1) == 0)
{
System.out.println("array Can be divided into two subset of equal sum");
sum = sum / 2;
subsetSumPath_util(arr,sum);
}
else
{ // if sum is odd then partition not possible
System.out.println("array Can not be divided into two subset of equal sum");
}
}
}