2226. Maximum Candies Allocated to K Children

Maximum Candies Allocated to K Children (LeetCode 2226) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Binary Search Patterns pattern, in the On Answer / Condition Function subpattern.

How to solve Maximum Candies Allocated to K Children

Maximum Candies Allocated to K Children is a Binary Search on Answer Space problem, part of Binary Search Patterns. Learn binary search on answer space for optimization problems like Koko eating bananas and capacity to ship packages.

Work towards the solution in these steps:

  1. For a fixed number of candies c, how can you check if each child can get c candies?
  2. Use binary search to find the maximum c as the answer.

The reference solution runs in O(nlogr) time and O(1) space.

The Maximum Candies Allocated to K Children solution and editorial walks through the full approach with reference code.

Problem statement

You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.

You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can be allocated candies from only one pile of candies and some piles of candies may go unused.

Return the maximum number of candies each child can get.

Example 1:

Input: candies = [5,8,6], k = 3 Output: 5 Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.

Example 2:

Input: candies = [2,5], k = 11 Output: 0 Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.

Constraints:

- 1 <= candies.length <= 105

- 1 <= candies[i] <= 107

- 1 <= k <= 1012

Topics and companies

Topics: Array, Binary Search.

How to practise Maximum Candies Allocated to K Children on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(nlogr) time and O(1) space.

Open Maximum Candies Allocated to K Children in the code editor, or read the Maximum Candies Allocated to K Children editorial for a worked solution with its approach and complexity analysis.

Where Maximum Candies Allocated to K Children sits in the DSA pattern sheet

Problems related to Maximum Candies Allocated to K Children

Other problems that use the same On Answer / Condition Function and Binary Search Patterns techniques:

Maximum Candies Allocated to K Children FAQ

What is the LeetCode number of Maximum Candies Allocated to K Children?

Maximum Candies Allocated to K Children is LeetCode problem 2226, rated Medium.

What pattern is used to solve Maximum Candies Allocated to K Children?

Maximum Candies Allocated to K Children is a Binary Search on Answer Space problem. It belongs to the Binary Search Patterns pattern, in the On Answer / Condition Function subpattern.

What is the time complexity of Maximum Candies Allocated to K Children?

The reference solution runs in O(nlogr) time and O(1) space.

Preparing this workspace.