Menu
CoddyTech

Chocolate Distribution Problem

You have some packets of chocolate, and packets[i] is the number of chocolates in packet i. Each of m students gets exactly one packet, so m different packets go out. Pick them so that the difference between the largest and the smallest packet handed out is as small as possible.

Write a function named minDifference that returns that smallest possible difference.

Function

minDifference(packets: integer-array, m: integer) → integer
packetsinteger-array
the number of chocolates in each packet
minteger
the number of students, each getting one packet
Returnsinteger
the smallest possible difference between the largest and smallest packet handed out

Constraints

  • 2 ≤ m ≤ packets.length ≤ 104
  • 1 ≤ packets[i] ≤ 105
  • Two packets can hold the same number of chocolates.

Examples

Input
packets = [9, 3, 15, 7, 11, 4]m = 3
Output
4
Explanation
Sorted, the packets are 3, 4, 7, 9, 11 and 15. Both 3, 4, 7 and 7, 9, 11 give a difference of 4, and no three packets are closer together.
Reset code
def minDifference(packets, m):
    # Write code here

Test #1

Test #2

Test #3

Arguments

packets = [9, 3, 15, 7, 11, 4]
m = 3

Expected Output

4