Skip to content

Commit ebf7f6a

Browse files
authored
Create Partition_Equal_Subset_Sum.cpp
1 parent cdc8643 commit ebf7f6a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://practice.geeksforgeeks.org/problems/subset-sum-problem2014/1
2+
3+
class Solution{
4+
public:
5+
6+
int equalPartition(int N, int arr[])
7+
{
8+
9+
int n = N;
10+
int sum = 0;
11+
for(int i = 0; i<n; i++)
12+
sum += arr[i];
13+
if(sum%2==0)
14+
{
15+
int dp[sum/2 + 1][n+1];
16+
memset(dp,-1,sizeof(dp));
17+
18+
for(int i = 0; i<=sum/2; i++)
19+
{
20+
for(int j = 0; j<=n; j++)
21+
{
22+
if(i==0)
23+
dp[i][j] = 1;
24+
else if(j==0)
25+
dp[i][j] = 0;
26+
else
27+
{
28+
dp[i][j] = dp[i][j-1];
29+
if(arr[j-1]<=i)
30+
dp[i][j] = dp[i][j] || dp[i-arr[j-1]][j-1];
31+
}
32+
}
33+
}
34+
return dp[sum/2][n];
35+
36+
}
37+
else
38+
{
39+
return 0;
40+
}
41+
}
42+
};

0 commit comments

Comments
 (0)