Given an integer array **A[ ]** of size **N**, the task is to find a subsequence of size **B** such that the minimum difference between any two of them is maximum and print this largest minimum difference.**Examples:**

Input:A[ ] = {1, 2, 3, 5}, B = 3Output:2Explanation:

Possible subsequences of size 3 are {1, 2, 3}, {1, 2, 5}, {1, 3, 5} and {2, 3, 5}.

For {1, 3, 5}, possible differences are (|1 – 3| = 2), (|3 – 5| = 2) and (|1 – 5| = 4), Minimum(2, 2, 4) = 2

For the remaining subsequences, the minimum difference comes out to be 1.

Hence, the maximum of all minimum differences is 2.

Input:A[ ] = {5, 17, 11}, B = 2Output:12Explanation:

Possible subsequences of size 2 are {5, 17}, {17, 11} and {5, 11}.

For {5, 17}, possible difference is (|5 – 17| = 12), Minimum = 12

For {17, 11}, possible difference is (|17 – 11| = 6), Minimum = 6

For {5, 11}, possible difference is (|5 – 11| = 6), Minimum = 6

Maximum(12, 6, 6) = 12

Hence, the maximum of all minimum differences is 12.

**Naive Approach:**

The simplest approach to solve this problem is to generate all possible subsequences of size **B** and find the minimum difference among all possible pairs. Finally, find the maximum among all the minimum differences.

**Time complexity:** O(2^{N}*N^{2})**Auxiliary Space:** O(N)

**Efficient Approach:**

Follow the steps below to optimize the above approach using Binary Search:

- Set the search space from
**0**to maximum element in the array(**maxm**) - For each calculated
**mid**, check whether it is possible to get a subsequence of size**B**with a*minimum difference among any pair equal to*.**mid** - If it is possible, then store
**mid**in a variable and find a better answer in the right half and discard the left half of the mid - Otherwise, traverse the left half of the mid, to check if a subsequence with smaller minimum difference of pairs exists.
- Finally, after termination of the binary search, print the
*highest*for which any subsequence with minimum difference of pairs equal to**mid****mid**was found.

Illustration:

A[ ] = {1, 2, 3, 4, 5}, B = 3Search space:{0, 1, 2, 3, 4, 5}

Steps involved in binary search are as follows:

- start = 0, end = 5, mid = (0 + 5) / 2 = 2

Subsequence of sizeBwith minimum difference ofmid(= 2) is {1, 3, 5}.

Therefore, ans = 2- Now, traverse the right half.

start = mid +1 = 3, end = 5, mid = (3 + 5) / 2 = 4

Subsequence of size B with minimum difference ofmid(= 4) is not possible.

Therefore, ans is still 2.- Now, traverse the left half

start = 3, end = mid – 1 = 3, mid = (3 + 3) / 2 = 3

Subsequence of sizeBwith minimum difference of mid(= 3) is not possible.

Therefore, ans is still 2.- Again, traverse left half.

start = 3, end = mid – 1 = 2.

Sincestartexceedsend, binary search teminates.- Finally, the largest possible minimum difference is 2.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check a subsequence can ` `// be formed with min difference mid ` `bool` `can_place(` `int` `A[], ` `int` `n, ` ` ` `int` `B, ` `int` `mid) ` `{ ` ` ` `int` `count = 1; ` ` ` `int` `last_position = A[0]; ` ` ` ` ` `// If a subsequence of size B ` ` ` `// with min diff = mid is possible ` ` ` `// return true else false ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `if` `(A[i] - last_position ` ` ` `>= mid) { ` ` ` `last_position = A[i]; ` ` ` `count++; ` ` ` `if` `(count == B) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Function to find the maximum of ` `// all minimum difference of pairs ` `// possible among the subsequence ` `int` `find_min_difference(` `int` `A[], ` ` ` `int` `n, ` `int` `B) ` `{ ` ` ` ` ` `// Sort the Array ` ` ` `sort(A, A + n); ` ` ` ` ` `// Stores the boundaries ` ` ` `// of the search space ` ` ` `int` `s = 0; ` ` ` `int` `e = A[n - 1] - A[0]; ` ` ` ` ` `// Store the answer ` ` ` `int` `ans = 0; ` ` ` ` ` `// Binary Search ` ` ` `while` `(s <= e) { ` ` ` ` ` `long` `long` `int` `mid = (s + e) / 2; ` ` ` ` ` `// If subsequence can be formed ` ` ` `// with min diff mid and size B ` ` ` `if` `(can_place(A, n, B, mid)) { ` ` ` `ans = mid; ` ` ` ` ` `// Right half ` ` ` `s = mid + 1; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// Left half ` ` ` `e = mid - 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `A[] = { 1, 2, 3, 5 }; ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` `int` `B = 3; ` ` ` ` ` `int` `min_difference ` ` ` `= find_min_difference(A, n, B); ` ` ` `cout << min_difference; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to check a subsequence can ` `// be formed with min difference mid ` `static` `boolean` `can_place(` `int` `A[], ` `int` `n, ` ` ` `int` `B, ` `int` `mid) ` `{ ` ` ` `int` `count = ` `1` `; ` ` ` `int` `last_position = A[` `0` `]; ` ` ` ` ` `// If a subsequence of size B ` ` ` `// with min diff = mid is possible ` ` ` `// return true else false ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(A[i] - last_position >= mid) ` ` ` `{ ` ` ` `last_position = A[i]; ` ` ` `count++; ` ` ` `if` `(count == B) ` ` ` `{ ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Function to find the maximum of ` `// all minimum difference of pairs ` `// possible among the subsequence ` `static` `int` `find_min_difference(` `int` `A[], ` ` ` `int` `n, ` `int` `B) ` `{ ` ` ` ` ` `// Sort the Array ` ` ` `Arrays.sort(A); ` ` ` ` ` `// Stores the boundaries ` ` ` `// of the search space ` ` ` `int` `s = ` `0` `; ` ` ` `int` `e = A[n - ` `1` `] - A[` `0` `]; ` ` ` ` ` `// Store the answer ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Binary Search ` ` ` `while` `(s <= e) ` ` ` `{ ` ` ` `int` `mid = (s + e) / ` `2` `; ` ` ` ` ` `// If subsequence can be formed ` ` ` `// with min diff mid and size B ` ` ` `if` `(can_place(A, n, B, mid)) ` ` ` `{ ` ` ` `ans = mid; ` ` ` ` ` `// Right half ` ` ` `s = mid + ` `1` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Left half ` ` ` `e = mid - ` `1` `; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `A[] = { ` `1` `, ` `2` `, ` `3` `, ` `5` `}; ` ` ` `int` `n = A.length; ` ` ` `int` `B = ` `3` `; ` ` ` ` ` `int` `min_difference = find_min_difference(A, n, B); ` ` ` ` ` `System.out.print(min_difference); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to check a subsequence can ` `# be formed with min difference mid ` `def` `can_place(A, n, B, mid): ` ` ` ` ` `count ` `=` `1` ` ` `last_position ` `=` `A[` `0` `] ` ` ` ` ` `# If a subsequence of size B ` ` ` `# with min diff = mid is possible ` ` ` `# return true else false ` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` `if` `(A[i] ` `-` `last_position >` `=` `mid): ` ` ` `last_position ` `=` `A[i] ` ` ` `count ` `=` `count ` `+` `1` ` ` ` ` `if` `(count ` `=` `=` `B): ` ` ` `return` `bool` `(` `True` `) ` ` ` ` ` `return` `bool` `(` `False` `) ` ` ` `# Function to find the maximum of ` `# all minimum difference of pairs ` `# possible among the subsequence ` `def` `find_min_difference(A, n, B): ` ` ` ` ` `# Sort the Array ` ` ` `A.sort() ` ` ` ` ` `# Stores the boundaries ` ` ` `# of the search space ` ` ` `s ` `=` `0` ` ` `e ` `=` `A[n ` `-` `1` `] ` `-` `A[` `0` `] ` ` ` ` ` `# Store the answer ` ` ` `ans ` `=` `0` ` ` ` ` `# Binary Search ` ` ` `while` `(s <` `=` `e): ` ` ` `mid ` `=` `(` `int` `)((s ` `+` `e) ` `/` `2` `) ` ` ` ` ` `# If subsequence can be formed ` ` ` `# with min diff mid and size B ` ` ` `if` `(can_place(A, n, B, mid)): ` ` ` `ans ` `=` `mid ` ` ` ` ` `# Right half ` ` ` `s ` `=` `mid ` `+` `1` ` ` ` ` `else` `: ` ` ` ` ` `# Left half ` ` ` `e ` `=` `mid ` `-` `1` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `A ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `5` `] ` `n ` `=` `len` `(A) ` `B ` `=` `3` ` ` `min_difference ` `=` `find_min_difference(A, n, B) ` ` ` `print` `(min_difference) ` ` ` `# This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to check a subsequence can ` `// be formed with min difference mid ` `static` `bool` `can_place(` `int` `[] A, ` `int` `n, ` ` ` `int` `B, ` `int` `mid) ` `{ ` ` ` `int` `count = 1; ` ` ` `int` `last_position = A[0]; ` ` ` ` ` `// If a subsequence of size B ` ` ` `// with min diff = mid is possible ` ` ` `// return true else false ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `if` `(A[i] - last_position >= mid) ` ` ` `{ ` ` ` `last_position = A[i]; ` ` ` `count++; ` ` ` `if` `(count == B) ` ` ` `{ ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Function to find the maximum of ` `// all minimum difference of pairs ` `// possible among the subsequence ` `static` `int` `find_min_difference(` `int` `[] A, ` ` ` `int` `n, ` `int` `B) ` `{ ` ` ` ` ` `// Sort the Array ` ` ` `Array.Sort(A); ` ` ` ` ` `// Stores the boundaries ` ` ` `// of the search space ` ` ` `int` `s = 0; ` ` ` `int` `e = A[n - 1] - A[0]; ` ` ` ` ` `// Store the answer ` ` ` `int` `ans = 0; ` ` ` ` ` `// Binary Search ` ` ` `while` `(s <= e) ` ` ` `{ ` ` ` `int` `mid = (s + e) / 2; ` ` ` ` ` `// If subsequence can be formed ` ` ` `// with min diff mid and size B ` ` ` `if` `(can_place(A, n, B, mid)) ` ` ` `{ ` ` ` `ans = mid; ` ` ` ` ` `// Right half ` ` ` `s = mid + 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Left half ` ` ` `e = mid - 1; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `[] A = { 1, 2, 3, 5 }; ` ` ` `int` `n = A.Length; ` ` ` `int` `B = 3; ` ` ` ` ` `int` `min_difference = find_min_difference(A, n, B); ` ` ` ` ` `Console.Write(min_difference); ` `} ` `} ` ` ` `// This code is contributed by rock_cool` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(NlogN)**Auxiliary Space:** O(1)

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:

- Product of all Subsequences of size K except the minimum and maximum Elements
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Maximum size of square such that all submatrices of that size have sum less than K
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Min difference between maximum and minimum element in all Y size subarrays
- Count subsequences which contains both the maximum and minimum array element
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Ways to form n/2 pairs such that difference of pairs is minimum
- Maximum number of tiles required to cover the floor of given size using 2x1 size tiles
- Subsequences of size three in an array whose sum is divisible by m
- Count number of increasing subsequences of size k
- Maximize product of same-indexed elements of same size subsequences
- Number of GP (Geometric Progression) subsequences of size 3
- Pair having all other given pairs lying between its minimum and maximum
- Construct array having X subsequences with maximum difference smaller than d
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Sum of minimum and maximum elements of all subarrays of size k.
- Minimum and Maximum of all subarrays of size K using Map

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.