human
Choose k array elements such that difference of maximum and minimum is minimized | Return minimum difference of maximum and minimum of k elements of arr [ 0. . n - 1 ] ..
Sorting the array ..
Find minimum value among all K size subarray ..
Driver code"
Solution in Python:
 What is a code to choose k array elements such that difference of maximum and minimum is minimized and the other steps required?
assistant
#Here is code to choose k array elements such that difference of maximum and minimum is minimized and the other steps required.
 def minDiff ( 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
arr = [ 10 , 100 , 300 , 200 , 1000 , 20 , 30 ]
n = len ( arr )
k = 3
print ( minDiff ( arr , n , k ) )