-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPartitionArray.java
More file actions
27 lines (25 loc) · 893 Bytes
/
PartitionArray.java
File metadata and controls
27 lines (25 loc) · 893 Bytes
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
/*https://practice.geeksforgeeks.org/problems/subset-sum-problem2014/1*/
/*
If there are no elements in arr, answer will be false
If sum is 0, answer is true
For all other cases, the answer will be true
if it is either true by including the current element or by excluding it
*/
class Solution{
static int equalPartition(int N, int arr[])
{
int sum = 0;
for (int i = 0; i < N; ++i)
sum += arr[i];
if (sum%2 != 0) return 0;
boolean[][] table = new boolean[(sum/2)+1][N+1];
for (int i = 0; i <= sum/2; ++i)
table[i][0] = false;
for (int i = 0; i <= N; ++i)
table[0][i] = true;
for (int i = 1; i <= sum/2; ++i)
for (int j = 1; j <= N; ++j)
table[i][j] = table[i][j-1]||(i >= arr[j-1] ? table[i-arr[j-1]][j-1] : false);
return table[sum/2][N] ? 1 : 0;
}
}