Showing:
Handle Score
@Ibrahim Nash 6564
@mb1973 5785
@Quandray 5275
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4639
@sanjay05 3762
@mantu_singh 3712
@gfgaccount 3627
@marius_valentin_dragoi 3525
Minimum Difference among K
Basic Accuracy: 52.04% Submissions: 911 Points: 1

Given an array of n integers and a positive number k. We are allowed to take any k integers from the given array. The task is to find the minimum possible value of the difference between maximum and minimum of k numbers.

Example 1:

Input:
N=7
K=3
arr[] = {10, 100, 300, 200, 1000, 20, 30}
Output:
20
Explanation:
20 is the minimum possible difference
between any maximum and minimum of any
k numbers.Given k = 3, we get the result
20 by selecting integers {10, 20, 30}.
max(10, 20, 30) - max(10, 20, 30) = 30 - 10
= 20.

Example 2:

Input:
N=2
K=2
arr[] = { 1,1 }
Output:
0
Explanation:
max({1,1}) - min({1,1}) = 1 - 1 = 0


Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function minDiff() that takes array arr, integer N and integer K as parameters and return the value of friendliness for the given array.

Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(1).

Constraints:
2 ≤ N ≤ 105