← DiffPush

Partition Equal Subset Sum

Standard Bar Dynamic Programming · DP on Subsequences O(n*S) · O(S)

The Twin Locker Allocation

A gym equips two identical lockers and must split a shipment of weighted plates between them so both lockers carry exactly the same load. Plates are rigid — no splitting in half. The intake scanner answers one question per shipment: can the plates be divided into two perfectly balanced groups?

Input: An array nums of n positive integers.

Output: Return true if nums can be split into two subsets with equal sums, otherwise false.

Constraints

Examples

Example 1

Input: {"nums":[1,5,11,5]}
Output: true
Locker A takes 11; locker B takes 1 + 5 + 5 = 11.

Example 2

Input: {"nums":[1,2,3,5]}
Output: false
The total 11 is odd, so equal halves are impossible.

Solve this in your browser →

Also on LeetCode ↗