Determine whether an array can be split into two subsets with equal total sum.
Given a list of positive integers, decide whether it is possible to partition the numbers into two groups such that the sum of the numbers in both groups is the same.
You may use each element at most once, and every element must belong to exactly one of the two groups.
Input Format
- An integer array
numscontaining positive integers.
Output Format
- Return
trueif the array can be partitioned into two subsets with equal sum. - Otherwise, return
false.
Constraints
1 <= nums.length- All values are positive integers
- The total sum determines whether an equal split is possible
- A valid partition must use every element exactly once
Example 1
Input
nums = [1,5,11,5]
Output
true
Explanation
The array can be split into [1, 5, 5] and [11]. Both subsets sum to 11.
Example 2
Input
nums = [1,2,3,5]
Output
false
Explanation
The total sum is 11, which is odd, so it cannot be split into two equal-sum subsets.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.