21. Maximize the Minimum Difference Between K Elements
β GFG solution to the Maximize the Minimum Difference Between K Elements problem: select k elements to maximize minimum absolute difference using binary search and greedy approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[]
of integers and an integer k
. Your task is to select k elements from the array such that the minimum absolute difference between any two of the selected elements is maximized. Return this maximum possible minimum difference.
The goal is to strategically choose k elements from the array to ensure that even the closest pair among the selected elements has the largest possible difference.
π Examples
Example 1
Input: arr[] = [2, 6, 2, 5], k = 3
Output: 1
Explanation: 3 elements out of 4 elements are to be selected with a minimum difference as large as possible.
Selecting 2, 2, 5 will result in minimum difference as 0.
Selecting 2, 5, 6 will result in minimum difference as 6 - 5 = 1.
Example 2
Input: arr[] = [1, 4, 9, 0, 2, 13, 3], k = 4
Output: 4
Explanation: Selecting 0, 4, 9, 13 will result in minimum difference of 4,
which is the largest minimum difference possible.
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$0 \le \text{arr}[i] \le 10^6$
$2 \le k \le \text{arr.size()}$
β
My Approach
The optimal approach uses Binary Search on Answer combined with a Greedy Algorithm:
Binary Search + Greedy Selection
Sort the Array:
First, sort the array to enable efficient greedy selection.
This allows us to consider elements in ascending order.
Binary Search Setup:
Set search range:
left = 0
(minimum possible difference) toright = max - min
(maximum possible difference).We binary search on the possible values of minimum difference.
Validation Function (Greedy):
For a given minimum difference
mid
, check if we can select k elements.Start with the first element, then greedily select the next element that is at least
mid
distance away.Continue until we either select k elements (valid) or exhaust the array (invalid).
Binary Search Logic:
If we can achieve difference
mid
with k elements, try for a larger difference (left = mid + 1
).If we cannot achieve difference
mid
, try for a smaller difference (right = mid - 1
).Store the maximum achievable difference as our answer.
Return Result:
The binary search converges to the maximum possible minimum difference.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n + n log(max-min)), where n is the size of the array. The sorting takes O(n log n), and binary search takes O(log(max-min)) iterations, each requiring O(n) validation.
Expected Auxiliary Space Complexity: O(1), as we only use constant additional space beyond the input array for sorting (in-place sorting).
π§βπ» Code (C++)
class Solution {
public:
int maxMinDiff(vector<int>& a, int k) {
sort(a.begin(), a.end());
int l = 0, r = a.back() - a[0], ans = 0;
while (l <= r) {
int m = l + (r - l) / 2;
int cnt = 1, last = a[0];
for (int i = 1; i < a.size() && cnt < k; i++)
if (a[i] - last >= m) cnt++, last = a[i];
cnt >= k ? ans = m, l = m + 1 : r = m - 1;
}
return ans;
}
};
β Code (Java)
class Solution {
public int maxMinDiff(int[] a, int k) {
Arrays.sort(a);
int l = 0, r = a[a.length - 1] - a[0], ans = 0;
while (l <= r) {
int m = l + (r - l) / 2;
int cnt = 1, last = a[0];
for (int i = 1; i < a.length && cnt < k; i++)
if (a[i] - last >= m) { cnt++; last = a[i]; }
if (cnt >= k) { ans = m; l = m + 1; } else r = m - 1;
}
return ans;
}
}
π Code (Python)
class Solution:
def maxMinDiff(self, a, k):
a.sort()
l, r, ans = 0, a[-1] - a[0], 0
while l <= r:
m = (l + r) // 2
cnt, last = 1, a[0]
for x in a[1:]:
if x - last >= m:
cnt += 1
last = x
if cnt == k: break
if cnt >= k:
ans, l = m, m + 1
else:
r = m - 1
return ans
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated