![]() |
VOOZH | about |
Given that cake consists of N chunks, whose individual sweetness is represented by the sweetness[] array, the task is to cut the cake into K + 1 pieces to maximize the minimum sweetness.
Examples:
Input: N = 6, K = 2, sweetness[] = {6, 3, 2, 8, 7, 5}
Output: 9
Explanation: Divide the cake into [6, 3], [2, 8], [7, 5] with sweetness levels 9, 10, and 12 respectively.Input: N = 7, K = 3, sweetness[] = {1, 2, 4, 7, 3, 6, 9}
Output: 7
Explanation: Divide the cake into [1, 2, 4], [7], [3, 6], [9] with sweetness levels 7, 7, 9, and 9 respectively.
Approach: This can be solved with the following idea:
The approach used is the binary search to find the maximum sweetness value that can be achieved after dividing the given array of sweetness values into k + 1 groups, where each group has a minimum sweetness value of mn_value.
Steps involved in the implementation of code:
Below is the implementation of the above approach:
9
Time Complexity: O(n * log(sum))
Auxiliary Space: O(1)