Ctrl K

The Fairest Way to Hand Out Bags

Medium
At a party, sweets are packed in bags: bag i holds bags[i] sweets. All bags must be handed out to kids children. A bag cannot be opened, so it goes to one child as a whole. A child may end up with no bags. The **imbalance** of a hand-out is the largest number of sweets any single child receives. Return the smallest possible imbalance.
Example 1
bags=[5, 9, 4, 6], kids=2 → 13

There are 24 sweets in total. One child gets the bags with 5 and 6, the other gets 9 and 4: that is 11 and 13. An even 12–12 split is impossible, so the answer is 13.

Example 2
bags=[3, 3, 3, 20, 3], kids=3 → 20

The bag of 20 cannot be split, so someone gets at least 20. The other bags go easily to the other two children.

Example 3
bags=[7, 2, 2, 3, 6, 4], kids=3 → 9
Constraints
  • 2 ≤ len(bags) ≤ 8
  • 1 ≤ bags[i] ≤ 10⁵
  • 2 ≤ kids ≤ len(bags)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare Frame From Sticks MediumSeating by Divisibility MediumCheapest Basket with Bundles MediumGroups With Equal Sums MediumLeast Total Walk to Scooters Medium
loading Python…
Input
bags=[5, 9, 4, 6], kids=2
Expected
13

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