Partition Equal Subset Sum

Medium

Given an array nums of positive integers, return true if you can split it into two groups with the same sum, and false otherwise.

Every number goes into exactly one of the two groups.

Examples

Example 1

Input:  nums = [1,5,11,5]
Output: true
Example 1: nums = [1,5,11,5]Group A155Group B11= 11= 11Same length, so the answer is true.

[1,5,5] and [11] both add up to 11.

Example 2

Input:  nums = [1,2,3,5]
Output: false

The numbers add up to 11. An odd total can't be split into two equal halves.

Example 3

Input:  nums = [1,2,5]
Output: false

The total is 8, so each group would have to add up to 4. No group of these numbers does: the possible sums are 1, 2, 3, 5, 6, 7 and 8. An even total isn't enough on its own.

Example 4

Input:  nums = [3,3,2,2,2]
Output: true
Example 4: nums = [3,3,2,2,2]A split that works33222Group AGroup B= 6= 6Biggest numbers first, each to the smaller group32232Group AGroup B= 7= 5

[3,3] and [2,2,2] both add up to 6. Handing out the biggest numbers first, each to the group with the smaller sum, puts the two 3s in different groups and ends at 7 and 5, so that approach would wrongly say false.

Example 5

Input:  nums = [7]
Output: false

One group gets the 7 and the other gets nothing, so the sums are 7 and 0.

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
TypeScript
Loading editor…
Test cases34
Case 1
nums = [1,5,11,5]
Case 2
nums = [1,2,3,5]
Case 3
nums = [1,2,5]
Case 4
nums = [3,3,2,2,2]
Case 5
nums = [7]
Case 6
nums = [1,1]
Case 7
nums = [1,2]
Case 8
nums = [100,100]
Case 9
nums = [100]
Case 10
nums = [2,2,3,5]
Case 11
nums = [1,2,3,4,5,6,7]
Case 12
nums = [14,9,8,4,3,2]
Case 13
nums = [100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100]
Case 14
nums = [100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100]
Case 15
nums = [4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2]
Case 16
nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Case 17
nums = [100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Case 18
nums = [99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1,99,1]
Case 19
nums = [54,30,48,18,54,36,66,36,18,84,24,66,12,90,96,72,12,24,90,84,66,96,60,84,90,54,96,30,54,12,24,12,42,84,54,66,78,36,12,6,84,72,54,78,12,30,6,42,66,24,84,6,24,6,42,72,84,24,24,36,90,18,66,42,48,84,72,96,78,54,30,18,90,66,30,78,90,48,48,42,48,96,48,6,90,6,18,48,36,96,18,42,96,12,60,12,42,96,48,90,36,60,36,54,54,30,12,36,42,12,48,18,18,90,96,96,42,84,6,48,66,12,6,42,96,90,12,78,42,24,66,72,66,36,72,24,48,66,90,54,48,90,60,12,66,90,66,60,72,30,90,72,78,48,84,54,96,96,72,12,72,60,66,90,72,78,72,6,90,90,42,96,42,72,12,66,30,54,12,24,60,48,36,54,48,96,72,48,66,60,42,90,18,60,66,24,18,6,24,4]
Case 20
nums = [48,90,6,24,78,30,90,90,42,54,54,54,60,24,6,18,24,96,72,24,6,12,60,78,84,66,54,96,12,90,84,72,84,72,24,78,90,42,96,72,72,48,30,84,48,60,48,84,24,84,90,78,18,12,84,66,84,48,60,84,96,72,96,84,24,36,30,18,66,54,18,30,48,6,96,48,72,72,60,42,72,72,78,36,54,42,24,66,12,90,42,90,60,48,60,24,72,84,12,42,36,54,54,42,18,72,36,90,84,66,42,84,6,66,72,30,96,36,66,54,48,84,18,36,48,18,84,42,6,6,96,30,42,48,6,60,72,12,12,90,96,84,60,60,18,12,60,66,12,60,96,72,78,84,30,30,84,24,36,12,48,6,30,72,18,66,48,48,60,66,36,6,72,96,84,72,48,48,90,30,6,12,48,36,12,90,36,60,90,6,90,54,6,18,42,30,6,24,2,4]
Case 21
nums = [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3]
Case 22
nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,100]
Case 23
nums = [3,3,3,4,5]
Case 24
nums = [1,3,4,4]
Case 25
nums = [2,3,4,6]
Case 26
nums = [74,6,14,97,16,18,90,18,8,64,63,65,38,14,73,45,9,55,96,60,74,37,70,89,95,95,94,42,81,73,58,79,67,44,61,78,81,55,81,93,63,38,3,57,69,25,87,31,63,32,84,99,3,16,19,79,74,26,96,51,53,3,64,34,14,91,18,29,81,96,10,73,53,56,26,62,74,38,6,85,72,2,34,94,53,76,25,40,30,54,91,52,18,26,9,27,40,32,90,17,61,51,40,68,29,33,82,86,83,42,49,94,1,12,67,59,25,21,69,14,21,37,57,1,16,90,73,40,2,33,39,1,34,30,13,34,70,78,77,67,62,74,35,83,30,67,74,95,57,12,92,96,69,59,31,93,25,58,81,36,97,71,23,66,82,92,82,56,15,7,53,17,11,58,36,64,52,63,50,82,2,60,85,5,52,5,80,28,35,5,36,86,39,12,78,84,62,21,50,80]
Case 27
nums = [12,69,5,11,16,69,65,94,82,92,10,30,5,3,83,2,96,54,73,55,17,30,32,97,32,29,17,20,70,75,33,80,53,64,93,30,31,100,90,31,41,70,58,82,29,86,88,5,95,82,93,91,80,72,34,82,36,59,96,32,27,91,52,93,86,97,31,11,82,34,64,63,77,96,19,52,4,92,30,79,51,22,33,30,18,1,5,42,11,57,1,66,12,4,76,53,89,100,64,15,5,36,23,34,68,58,7,72,92,93,53,32,5,5,83,13,89,27,92,72,13,36,39,85,8,74,47,15,64,71,18,12,87,82,43,10,12,27,89,47,59,31,36,9,90,71,89,26,26,8,49,15,67,60,45,68,84,73,80,30,51,70,66,38,67,77,53,26,95,69,60,65,44,15,63,83,57,26,34,31,41,7,24,96,34,64,81,59,42,42,40,53,74,20,54,57,97,99,92,91]
Case 28
nums = [72,77,93,8,87,84,61,91,56,26,17,81,67,67,97,6,18,32,33,9,30,94,96,79,37,63,6,33,2,51,83,79,50,99,84,47,60,50,10,52,56,63,77,31,28,19,77,89,85,58,78,98,22,57,64,53,35,99,46,43,47,4,76,85,47,17,18,20,6,40,81,65,35,25,26,47,18,34,39,9,85,47,21,70,33,67,67,43,11,92,21,72,32,65,94,40,47,55,43,38,21,18,60,31,34,42,28,39,90,1,49,11,87,17,2,82,52,30,52,76,95,19,63,97,25,71,30,94,29,16,29,25,51,49,38,7,20,54,50,74,51,56,40,25,79,11,91,27,15,39,98,92,43,52,77,94,71,3,95,23,22,36,62,98,98,91,7,99,32,4,86,2,67,39,5,12,18,52,16,54,78,53,82,42,67,88,62,8,96,70,99,45,69,19,93,94,11,53,90,82]
Case 29
nums = [5,7,37]
Case 30
nums = [9,21,8]
Case 31
nums = [11,9,5,10]
Case 32
nums = [10,2,2,98,4,91,7,51,34,1,4]
Case 33
nums = [9,84,2,25,95,93,7,5,1,2]
Case 34
nums = [87,7,96]