> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/01-backpack/416.-partition-equal-subset-sum.md).

# 416. Partition Equal Subset Sum

![](/files/-MkNA2dkLOUP6jTBNKCY)

### 01 背包問題

<https://labuladong.github.io/algo/3/27/81/>

## transform problem into 01 **knapsack**

<https://labuladong.github.io/algo/3/27/82/>

time: (n\*m), n is nums size, m = sum/2

space: (n\*m)

```java
class Solution {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        for (int num : nums) { // add them all
            sum += num;
        }
        
        if (sum % 2 == 1) { // if two sets are equal, sum must be even
            return false;
        }
        
        int n = nums.length;
        int m = sum/2; // become 01 backpack problems, target is sum/2
        
        boolean dp[][] = new boolean[n+1][m+1];
        
        dp[0][0] = true;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (j >= nums[i-1]) {
                    dp[i][j] = dp[i-1][j] || dp[i-1][j - nums[i-1]];
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[n][m]; // when sum/2 (two sets are equal), is it true?
    }
}
```

## 1D space

T: O(mn)

S: O(n)

```java
class Solution {
    public boolean canPartition(int[] nums) {
        
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        if (sum % 2 != 0) {
            return false;
        }
        int m = nums.length;
        int n = sum/2;
        boolean[] dp = new boolean[n+1]; // 0 ~ m number, 0~n weight
        
        dp[0] = true;
        for (int i = 1; i <= m; i++) {
            for (int j = n; j >= 0; j--) { // when go to 1D, should reverse to do, or will become complete kapnack
                
                int curr = nums[i-1]; // i-1 !
                if (j >= curr) {
                    // choose or not choose
                    dp[j] |= dp[j - curr];
                }
            }
        }
        return dp[n]; // we want sum/2
    }
}ja
```

或者把 j >= nums\[i-1] 寫在 for loop 就可以了, 少一個 if 條件

````java
```java
class Solution {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        if (sum % 2 != 0) {
            return false;
        }

        int m = nums.length;
        int n = sum/2;
        boolean[] dp = new boolean[n+1];
        dp[0] = true; // sum == 0, always true

        for (int i = 0; i < m; i++) {
            for (int j = n; j >= nums[i]; j--) {
                dp[j] = dp[j] ||  dp[j - nums[i]];
            }
        }
        return dp[n];
    }
}
```
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/01-backpack/416.-partition-equal-subset-sum.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
