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) - min(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
Your Task:
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 ≤ 10^{5}

Difficulty: EASY
Tags: Sorting

Data structures
Skill types: Sorting

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

Starter code:
#User function Template for python3





class Solution:

    def minDiff(self,arr,N,K):



Solutions:
class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		err = arr[-1] - arr[0]
		for i in range(len(arr)):
			if i + k - 1 < len(arr) and arr[i + k - 1] - arr[i] < err:
				err = arr[i + k - 1] - arr[i]
		return err


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		diff = []
		i = 0
		while i <= N - K:
			d = arr[i + K - 1] - arr[i]
			diff.append(d)
			i = i + 1
		j = min(diff)
		return j


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		min_diff = float('inf')
		for i in range(N - K + 1):
			diff = arr[i + K - 1] - arr[i]
			if diff < min_diff:
				min_diff = diff
		return min_diff


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		min = abs(arr[0] - arr[0 + K - 1])
		for i in range(N - K + 1):
			if abs(arr[i] - arr[i + K - 1]) <= min:
				min = abs(arr[i] - arr[i + K - 1])
		return min


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		mini = float('inf')
		for i in range(N - K + 1):
			j = i + K - 1
			mini = min(mini, arr[j] - arr[i])
		return mini


import heapq

class Solution:

	def minDiff(self, arr, N, K):
		heapq.heapify(arr)
		l = []
		for i in range(n):
			l.append(heapq.heappop(arr))
		x = 922337203685
		for i in range(n - k + 1):
			x = min(x, l[i + k - 1] - l[i])
		return x


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		ans = arr[N - 1]
		for i in range(K - 1, N):
			ans = min(ans, arr[i] - arr[i - K + 1])
		return ans


import sys

class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		res = sys.maxsize
		for i in range(N - K + 1):
			res = min(res, abs(arr[i + k - 1] - arr[i]))
		return res


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		res = arr[N - 1] - arr[0]
		for i in range(N - K + 1):
			res = min(res, arr[i + k - 1] - arr[i])
		return res


class Solution:

	def minDiff(self, arr, N, K):
		result = +999999
		arr.sort()
		for i in range(0, n - K + 1):
			result = int(min(result, arr[i + K - 1] - arr[i]))
		return result


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		min_diff = arr[K - 1] - arr[0]
		for i in range(K, N):
			curr_diff = arr[i] - arr[i - K + 1]
			min_diff = min(min_diff, curr_diff)
		return min_diff


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		min_diff = arr[k - 1] - arr[0]
		for i in range(k, n):
			curr_diff = arr[i] - arr[i - k + 1]
			if curr_diff < min_diff:
				min_diff = curr_diff
		return min_diff


import sys

class Solution:

	def minDiff(self, arr, N, K):
		ans = sys.maxsize
		arr.sort()
		for i in range(N - K + 1):
			ans = int(min(ans, arr[i + k - 1] - arr[i]))
		return ans


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		res = float('inf')
		(left, right) = (0, K - 1)
		while right < len(arr):
			res = min(res, arr[right] - arr[left])
			left += 1
			right += 1
		return res


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		res = arr[len(arr) - 1]
		for i in range(len(arr) - (K - 1)):
			if arr[i + K - 1] - arr[i] < res:
				res = arr[i + K - 1] - arr[i]
		return res


class Solution:

	def minDiff(self, arr, N, K):
		result = +2147483647
		arr.sort()
		for i in range(N - K + 1):
			result = int(min(result, arr[i + k - 1] - arr[i]))
		return result


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		min_d = arr[N - 1] - arr[0]
		for i in range(N - K + 1):
			d = arr[i + K - 1] - arr[i]
			if d < min_d:
				min_d = d
		return min_d


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		(l, r) = (0, K - 1)
		res = float('inf')
		while r < len(arr):
			res = min(res, arr[r] - arr[l])
			l += 1
			r += 1
		return res


class Solution:

	def minDiff(self, arr, N, K):
		arr.sort()
		res = []
		for i in range(N - K + 1):
			d = arr[i + K - 1] - arr[i]
			res.append(d)
		return min(res)


class Solution:

	def minDiff(self, arr, N, K):
		if N < K:
			return -1
		arr.sort()
		m = arr[K - 1] - arr[0]
		for i in range(N - K + 1):
			z = arr[i + K - 1] - arr[i]
			if z < m:
				m = z
		return m


class Solution:

	def minDiff(self, arr, N, k):
		arr.sort()
		d = []
		for i in range(N - k + 1):
			b = arr[i + k - 1] - arr[i]
			d.append(b)
		return min(d)