forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionProblem.java
More file actions
46 lines (41 loc) · 1.54 KB
/
PartitionProblem.java
File metadata and controls
46 lines (41 loc) · 1.54 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
package com.geeksforgeeks.dynamicProgramming;
/**
* https://www.geeksforgeeks.org/dynamic-programming-set-18-partition-problem/
* <p>
* arr[] = {1, 5, 11, 5}
* Output: true
* The array can be partitioned as {1, 5, 5} and {11}
* <p>
* arr[] = {1, 5, 3}
* Output: false
* The array cannot be partitioned into equal sum sets.
* <p>
* Solution: If Sum of Elements of Array is odd you can not have 2 subset which contribute to the sum
* if Sum of Elements of Array is even then we have to find a subset which sums upto Sum_Of_Elements_Of_Array / 2;
* <p>
* So In even case it becomes Subset Sum Problem
*/
public class PartitionProblem {
public static void main(String[] args) {
System.out.println("Is Partition Possible " + isPartitionPossible(new int[]{1, 5, 11, 5}));
System.out.println("Is Partition Possible " + isPartitionPossible(new int[]{1, 5, 3}));
System.out.println("Is Partition Possible " + isPartitionPossible(new int[]{10,10,10,320}));
}
public static Boolean isPartitionPossible(int[] arr) {
int Sum_Of_Elements_Of_Array = getSum(arr);
Boolean isPartitionPossible;
if (Sum_Of_Elements_Of_Array % 2 == 0) {
isPartitionPossible = SubsetSumProblem.isSubsetPresent(arr, Sum_Of_Elements_Of_Array / 2);
} else {
isPartitionPossible = false;
}
return isPartitionPossible;
}
public static int getSum(int[] values) {
int sum = 0;
for (int value : values) {
sum += value;
}
return sum;
}
}