AlgoMaster Logo

Partition Equal Subset Sum

nums=[1, 5, 11, 5]
1public boolean canPartition(int[] nums) {
2    int total = 0;
3    for (int num : nums) {
4        total += num;
5    }
6    if (total % 2 != 0) {
7        return false;
8    }
9
10    int target = total / 2;
11    boolean[] dp = new boolean[target + 1];
12    dp[0] = true;
13
14    for (int num : nums) {
15        for (int j = target; j >= num; j--) {
16            if (dp[j - num]) {
17                dp[j] = true;
18            }
19        }
20    }
21
22    return dp[target];
23}
0 / 46
15115NumsDP Array (Can Make Sum)