Pair Up to a Total
Form pairs of items of nums whose two items add up to exactly total. Every item may be used in at most one pair, and items are identified by position, so two equal values at different positions are two separate items. Return the largest number of pairs you can form.
Sorting the items and walking one index in from each end matches the pairs greedily: a sum that is too small moves the left index up, a sum that is too large moves the right index down, and a hit takes both items and moves both indices.
Example 1
1 + 4 and 2 + 3 both make 5, using every item once.
Example 2
Only 3 + 3 makes 6, and once two of the 3s are paired the third has no partner.
Constraints
0 <= len(nums) <= 100-1000 <= nums[i] <= 1000-2000 <= total <= 2000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.