Two Teams of Equal Size
nums has an even number of items. Split the items into two teams with the same number of items, so that the totals of the two teams are as close to each other as possible.
Return the smallest possible difference between the two totals.
Example 1
nums [8,1,6,2]
returns 1
The teams 8 + 1 = 9 and 6 + 2 = 8 differ by 1.
Example 2
nums [5,-5]
returns 10
Each team has one item, so the totals are 5 and -5.
Example 3
nums [1,2,3,4,5,6]
returns 1
For example 1 + 4 + 6 = 11 against 2 + 3 + 5 = 10. The total 21 is odd, so 0 is out of reach.
Constraints
2 <= len(nums) <= 30, and the length is even-107 <= nums[i] <= 107
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Sorting and searching problems
- Height CheckerBeginner
- Kth Largest ElementBeginner
- Kth Smallest Distinct ValueBeginner
- Maximum Product of Two ElementsBeginner
- Median of the ListBeginner
- Names in OrderBeginner
- Shortest Words FirstBeginner
- Sort by Last DigitBeginner