Design Gurus Logo
Fair Distribution of Cookies (medium)

Problem Statement

You are given an array of integers cookies, where cookies[i] represents the count of cookies in the i<sup>th</sup> bag, and integer k that represents the number of children. You must divide these bags among k children such that every bag goes entirely to one child, with no splitting of the contents.

The maximum number of total cookies obtained by a single child in the distribution is called the unfairness of the distribution.

Return the minimum unfairness of all distributions.

Examples

  1. Example 1:

    • Input: cookies = [5,6,7,3,4], k = 2
    • Expected Output: 13
    • Justification: The most balanced way to distribute the cookies is to give bags [5,7] to one child and [6,3,4] to the other, resulting in one child getting 13 cookies and the other getting 12. The maximum number of cookies received by a child is 13, which is the minimal unfairness possible here.
  2. Example 2:

    • Input: cookies = [1,2,3,4,5,6,7,8], k = 3
    • Expected Output: 12
    • Justification: An optimal distribution would give bags [4, 8], [5, 7], and [1, 2, 3, 6] to the three children. This way, the maximum cookies any child gets is 12, which is the best minimal unfairness achievable with this setup.
  3. Example 3:

    • Input: cookies = [10,10,10,10], k = 4
    • Expected Output: 10
    • Justification: Since there are equal numbers of cookies in each bag and the same number of children as there are bags, the fairest distribution is straightforward: each child gets one bag. The maximum cookies received by any child is 10, which is the lowest possible unfairness.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory