#K59497. Candy Grouping Challenge
Candy Grouping Challenge
Candy Grouping Challenge
You are given N candies, each with a specific size. Your task is to distribute all the candies into groups while satisfying the following conditions:
- Each group can contain at most G candies.
- The difference between the smallest and largest candy in the same group must be at most M, i.e., \(\text{max} - \text{min} \leq M\).
You may rearrange the candies in any order. Determine the minimum number of groups needed to distribute all the candies.
Note: When forming each group, choose as many candies as possible (up to G) that satisfy the condition on the size difference.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains three integers N, G, and M separated by spaces. Here, N is the total number of candies, G is the maximum number of candies allowed in a group, and M is the maximum allowed difference between the smallest and largest candy in a group.
- The second line contains N integers representing the sizes of the candies.
outputFormat
Output a single integer to stdout representing the minimum number of groups required to distribute all the candies.
## sample7 3 2
4 1 5 3 2 8 6
3
</p>