A courier loads parcels into a van whose scale only accepts an exact load, say 50 kg. Can some of today's parcels add up to exactly that weight, and which ones? On another day two vans share the parcels, and the courier wants the two loads as equal as possible.
Each function takes nums, an array of non-negative integers (the parcel weights). A subset uses each parcel at most once.
hasSubsetSum(nums, target) returns true if some subset of nums adds up to exactly target.
subsetWithSum(nums, target) returns the indexes of one such subset, in increasing order, or null if none exists. Any valid subset is accepted.
closestSplit(nums) divides all the numbers into two groups and returns the smallest possible difference between the two group totals.
Rules:
target is a non-negative integer. A target of 0 is reached by the empty subset, so subsetWithSum(nums, 0) returns [].
An empty nums reaches only 0, and closestSplit([]) is 0.
Throw a TypeError if nums is not an array of non-negative integers or target is not a non-negative integer.
nums can hold 200 numbers whose total is at most 200,000. Checking every subset takes 2²⁰⁰ steps; aim for O(n × target) time.