Ctrl K

Even Load of k Boxes

Medium
A loader must take exactly count boxes from the warehouse. The box weights are given in the array weights. The cart only moves when the total weight on it is even. Return the largest even total weight you can get by choosing exactly count different boxes. If no even total is possible, return -1.
Example 1
weights=[7, 2, 9, 4, 1], count=2 → 16

The two heaviest boxes, 9 and 7, give 16, which is even, so nothing beats it.

Example 2
weights=[8, 5, 6, 3], count=3 → 16

The three heaviest, 8, 6 and 5, sum to an odd 19. Swapping 5 for 3 keeps it odd, but swapping 6 for 3 gives 8 + 5 + 3 = 16.

Example 3
weights=[3, 5, 7], count=3 → -1

All three must be taken and their total 15 is odd, so the answer is -1.

Constraints
  • 1 ≤ len(weights) ≤ 10⁵
  • 0 ≤ weights[i] ≤ 10⁵
  • 1 ≤ count ≤ len(weights)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
weights=[7, 2, 9, 4, 1], count=2
Expected
16

Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.