AceYourInterviews
Checking sign-in…

138. Partition Equal Subset Sum

Intermediate0/1 subset reachability with descending amount updates

Assign every input occurrence to one of two subsets so that the two totals are equal. The subsets do not need to be contiguous or equal in size.

Each occurrence must belong to exactly one side. Equal values at different positions are separate occurrences, and empty sides follow the course's stated rules.

Example 1

Input

nums=[1,5,11,5]

Output

true

Explanation: Select the 11 occurrence; the complement 1+5+5 also totals 11.

Example 2

Input

nums=[1,2,3,5]

Output

false

Explanation: The total is odd, so equal integer totals are impossible.

Example 3

Input

nums=[1,2,5]

Output

false

Explanation: The total is eight, but no subset of distinct occurrences sums to four.

Example 4

Input

nums=[]

Output

true

Explanation: Two empty subsets both sum to zero.

Constraints

  • 0 <= nums.length <= 200
  • 0 <= nums[i] <= 1000
Hints
Hint 1

If one side totals T, what must the complement total?

Hint 2

Use a boolean exact-sum table for processed occurrences, not reusable types.

Hint 3

Read the previous occurrence layer in place by scanning target sums downward.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Descending updates prevent occurrence reuse

[1,2,5]
false