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.

Examples:

Input : arr[] = {10, 100, 300, 200, 1000, 20, 30} k = 3 Output : 20 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. Input : arr[] = {1, 2, 3, 4, 10, 20, 30, 40, 100, 200}. k = 4 Output : 3

The idea is to sort the array and choose k continuous integers. Why continuous? let the chosen k integers be arr[0], arr[1],,..arr[r], arr[r+x]…, arr[k-1], all in increasing order but not continuous in the sorted array. This means there exists an integer p which lies between arr[r] and arr[r+x],. So if p is included and arr[0] is removed, then the new difference will be arr[r] – arr[1] whereas old difference was arr[r] – arr[0]. And we know arr[0] <= arr[1] <= ..<= arr[k-1] so minimum difference reduces or remain same. If we perform same procedure for other p like number, we get the minimum difference.

Algorithm to solve the problem:

- Sort the Array.
- Calculate the maximum(k numbers) – minimum(k numbers) for each group of k consecutive integers.
- Return minimum of all values obtained in step 2.

Below is the implementation of above idea :

## C++

`// C++ program to find minimum difference of maximum ` `// and minimum of K number. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return minimum difference of maximum and minimum ` `// of k elements of arr[0..n-1]. ` `int` `minDiff(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `result = INT_MAX; ` ` ` ` ` `// Sorting the array. ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// Find minimum value among all K size subarray. ` ` ` `for` `(` `int` `i=0; i<=n-k; i++) ` ` ` `result = min(result, arr[i+k-1] - arr[i]); ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = {10, 100, 300, 200, 1000, 20, 30}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `int` `k = 3; ` ` ` ` ` `cout << minDiff(arr, n, k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum difference ` `// of maximum and minimum of K number. ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `// Return minimum difference of ` `// maximum and minimum of k ` `// elements of arr[0..n-1]. ` `static` `int` `minDiff(` `int` `arr[], ` `int` `n, ` `int` `k) { ` ` ` `int` `result = Integer.MAX_VALUE; ` ` ` ` ` `// Sorting the array. ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Find minimum value among ` ` ` `// all K size subarray. ` ` ` `for` `(` `int` `i = ` `0` `; i <= n - k; i++) ` ` ` `result = Math.min(result, arr[i + k - ` `1` `] - arr[i]); ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `arr[] = {` `10` `, ` `100` `, ` `300` `, ` `200` `, ` `1000` `, ` `20` `, ` `30` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `3` `; ` ` ` ` ` `System.out.println(minDiff(arr, n, k)); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find minimum ` `# difference of maximum ` `# and minimum of K number. ` ` ` `# Return minimum difference ` `# of maximum and minimum ` `# of k elements of arr[0..n-1]. ` `def` `minDiff(arr,n,k): ` ` ` `result ` `=` `+` `2147483647` ` ` ` ` `# Sorting the array. ` ` ` `arr.sort() ` ` ` ` ` `# Find minimum value among ` ` ` `# all K size subarray. ` ` ` `for` `i ` `in` `range` `(n` `-` `k` `+` `1` `): ` ` ` `result ` `=` `int` `(` `min` `(result, arr[i` `+` `k` `-` `1` `] ` `-` `arr[i])) ` ` ` ` ` `return` `result ` ` ` `# Driver code ` ` ` `arr` `=` `[` `10` `, ` `100` `, ` `300` `, ` `200` `, ` `1000` `, ` `20` `, ` `30` `] ` `n ` `=` `len` `(arr) ` `k ` `=` `3` ` ` `print` `(minDiff(arr, n, k)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// difference of maximum and ` `// minimum of K number. ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Return minimum difference of ` `// maximum and minimum of k ` `// elements of arr[0..n - 1]. ` `static` `int` `minDiff(` `int` `[]arr, ` `int` `n, ` ` ` `int` `k) ` `{ ` ` ` `int` `result = ` `int` `.MaxValue; ` ` ` ` ` `// Sorting the array. ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Find minimum value among ` ` ` `// all K size subarray. ` ` ` `for` `(` `int` `i = 0; i <= n - k; i++) ` ` ` `result = Math.Min(result, arr[i + k - 1] - arr[i]); ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() { ` ` ` `int` `[]arr = {10, 100, 300, 200, 1000, 20, 30}; ` ` ` `int` `n = arr.Length; ` ` ` `int` `k = 3; ` ` ` ` ` `Console.WriteLine(minDiff(arr, n, k)); ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// difference of maximum and ` `// minimum of K number. ` ` ` `// Return minimum difference ` `// of maximum and minimum ` `// of k elements of arr[0..n-1]. ` `function` `minDiff(` `$arr` `, ` `$n` `, ` `$k` `) ` `{ ` ` ` `$INT_MAX` `= 2147483647; ` ` ` `$result` `= ` `$INT_MAX` `; ` ` ` ` ` `// Sorting the array. ` ` ` `sort(` `$arr` `, ` `$n` `); ` ` ` `sort(` `$arr` `); ` ` ` ` ` `// Find minimum value among ` ` ` `// all K size subarray. ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= ` `$n` `- ` `$k` `; ` `$i` `++) ` ` ` `$result` `= min(` `$result` `, ` `$arr` `[` `$i` `+ ` `$k` `- 1] - ` ` ` `$arr` `[` `$i` `]); ` ` ` `return` `$result` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(10, 100, 300, 200, 1000, 20, 30); ` ` ` `$n` `= sizeof(` `$arr` `); ` ` ` `$k` `= 3; ` ` ` `echo` `minDiff(` `$arr` `, ` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output:

20

**Time Complexity: **O(nlogn).

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Choose an integer K such that maximum of the xor values of K with all Array elements is minimized
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Select K elements from an array whose maximum value is minimized
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Pair formation such that maximum pair sum is minimized
- Choose n elements such that their mean is maximum
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Number of ways to choose elements from the array such that their average is K
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Partition the array into two odd length groups with minimized absolute difference between their median
- Find a point such that sum of the Manhattan distances is minimized
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Minimum difference between maximum and minimum value of Array with given Operations
- Split array into two subarrays such that difference of their maximum is minimum
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Find an index such that difference between product of elements before and after it is minimum