05. Chocolate Distribution Problem
The problem can be found at the following link: Question Link
My Approach
To find the minimum difference between the maximum and minimum chocolates distributed to students, I used a greedy by sorting the given array of chocolates. Then, we take a window of size m
and slide it over the sorted array. We keep track of the minimum difference encountered during this process.
Time and Auxiliary Space Complexity
Time Complexity :
O(n log n)
time, wheren
is the number of elements in the input arraya
.Auxiliary Space Complexity :
O(1)
as we are using a constant amount of extra space.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated