#2616
Minimize the Maximum Difference of Pairs
specialist · 865 · lc medium +31 · verified · 50.9% accepted · 2,921 likes · top 39%
Description
You are given a 0-indexed integer array nums and an integer p. Select p pairs of indices (each index used at most once) to minimize the largest absolute difference among the selected pairs. Return that minimum possible maximum difference. The maximum of an empty set is zero.
Example 1:
Input: nums = [10,1,2,7,1,3], p = 2
Output: 1
Explanation: The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5.
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.
Example 2:
Input: nums = [4,2,1,2], p = 1
Output: 0
Explanation: Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.
Code
1
2
3