![]() |
VOOZH | about |
Given an array arr[] of integers, select k elements such that the minimum absolute difference between any two of the selected elements is as large as possible.
Out of all possible combinations of k elements from the array, choose the one where the smallest absolute difference between any pair of selected elements is the maximum possible across all combinations.
Return this maximum possible value of the minimum absolute difference.
Examples:
Input: arr = [2, 6, 2, 5], k = 3
Output: 1
Explanation: One possible selection is [2, 5, 6], where the minimum difference between any two is 1. No selection of 3 elements can give a higher minimum gap.Input: arr = [1, 4, 9, 0, 2, 13, 3], k = 4
Output: 4
Explanation: Selecting [0, 4, 9, 13] gives minimum pairwise difference 4, which is the largest possible under the given constraints.
Table of Content
The idea is to use top-down DP with memoization to explore all combinations of selecting k elements from the sorted array.
Sorting helps ensure that differences are non-negative and simplifies computing abs(arr[i] - arr[prev]) as just arr[i] - arr[prev].
The DP state dp[i][remk][prev] stores the maximum achievable minimum absolute difference when starting from index i, having selected remk elements so far, and the last selected index as prev. At each step, we either skip the current element or take it and update the minimum difference.
We return the maximum of the two choices. This is a DP problem due to optimal substructure and overlapping subproblems, which we avoid recomputing using memoization.
4
Time Complexity: O(n × k × n), we use a 3D DP with states defined by i (index), remk (picked count), and prev (last picked index). Each state is computed once and does constant work, giving a total of O(n × k × n) states.
Auxiliary Space: O(n × n × k)
We want to select k elements such that the minimum absolute difference between any two selected elements is maximized.
To find the best such minimum difference, we binary search on the answer space (possible min differences).
For each candidate difference mid, we greedily try to pick k elements such that the absolute difference between every consecutive pick is at least mid.
Sorting the array first is essential because it ensures that once we pick a number, we only need to check increasing values for valid selections.
Why the Problem is Monotonic (in Binary Search + Greedy):
This problem exhibits monotonic behavior in terms of the minimum difference (mid) we are trying to maximize — which makes it a perfect candidate for binary search on the answer.
4
Time Complexity: O(n log n + n log D) the array is first sorted in O(n log n) time. Then, binary search is applied on the possible minimum difference values ranging from 0 to D = max(arr) - min(arr), which gives O(log D) iterations. In each iteration, the isPossible function performs a single linear scan of the array in O(n) time.
Auxiliary Space: O(1), the solution uses only a constant number of variables regardless of input size, so the extra space usage is constant.