Menu
CoddyTech

Partition Equal Subset Sum

You get an array nums of positive integers. Decide whether you can split the values into two groups whose sums are equal. Every value goes into exactly one group, and a group can take values from any positions. Return true if such a split exists and false otherwise.

Function

canPartition(nums: integer-array) → boolean
numsinteger-array
the positive values to split into two groups
Returnsboolean
true when the values can form two groups with equal sums, false otherwise

Constraints

  • 1 ≤ nums.length ≤ 200
  • 1 ≤ nums[i] ≤ 100

Examples

Input
nums = [6, 1, 4, 9, 2]
Output
true
Explanation
The total is 22, so each group needs 11. The groups 9 + 2 and 6 + 1 + 4 both make 11, so the answer is true.

lock icon+18 hidden tests on Submit

challenge icon

Follow-up

When no equal split exists, can you return the smallest possible difference between the two group sums?

Reset code
def canPartition(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [6, 1, 4, 9, 2]

Expected

true