🎁 Exclusive Offer! Join our
Telegram Channel
to get **special discounts** and updates! 🚀
Question 83 - 100% Working Solution | Buy Now
Description
8 Live
Problem Statement
You are given an array arr of positive integers of size n. Each value in the array represents the number of toffees in a packet. Each packet can have any number of toffees. For example, if arr = [2, 3], this means that the first packet has 2 toffees and the second has 3 toffees. There are x students. The task is to distribute toffee packets among x students such that:
- Each student gets exactly one packet.
- The difference between the maximum number of toffees given to a student and the minimum number of toffees given to a student is minimized.
Input & Output
Input Format:
- The first line is an integer n, which represents the number of chocolate packets.
- The next line is an integer x, which represents the number of students.
- The next line contains n space-separated integer values that represent the number of toffees in each packet.
Input Constraints:
- n > x > 1
Output Format:
- An integer representing the difference between the maximum and minimum number of toffees given to a student.
Example 1:
Input:
7
3
7 3 2 4 9 12 56
Output:
2
Explanation:
Imagine Student 1 got the 2nd packet, Student 2 got the 3rd packet, and Student 3 got the 4th packet. So, the three students get 3, 2, and 4 toffees respectively.
The difference between the maximum (4) and minimum (2) number of toffees is 2. For no other distribution will this difference be smaller.