> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimize the Maximum Difference of Pairs

> Tested Python solution for LeetCode 2616 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2616, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/minimize-the-maximum-difference-of-pairs/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2616   # by problem number
lcpy gen -s minimize_the_maximum_difference_of_pairs   # by problem name
```

## Problem

You are given a **0-indexed** integer array `nums` and an integer `p`. Find `p` pairs of indices of `nums` such that the **maximum** difference amongst all the pairs is **minimized**. Also, ensure no index appears more than once amongst the `p` pairs.

Note that for a pair of elements at the index `i` and `j`, the difference of this pair is `|nums[i] - nums[j]|`, where `|x|` represents the **absolute** **value** of `x`.

Return *the **minimum** **maximum** difference among all* `p` *pairs.* We define the maximum of an empty set to be zero.

### Examples

```
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.
```

```
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.
```

### Constraints

* 1 \<= nums.length \<= 10^5
* 0 \<= nums\[i] \<= 10^9
* 0 \<= p \<= nums.length / 2

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimize_the_maximum_difference_of_pairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimize_the_maximum_difference_of_pairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n + n log m) where m = max(nums) - min(nums)
    # Space: O(n) for the sorted copy
    def minimize_max(self, nums: list[int], p: int) -> int:
        vals = sorted(nums)
        n = len(vals)

        def can_pair(target: int) -> bool:
            count = 0
            i = 0
            while i < n - 1:
                if vals[i + 1] - vals[i] <= target:
                    count += 1
                    i += 2
                else:
                    i += 1
            return count >= p

        low, high = 0, vals[-1] - vals[0]
        while low < high:
            mid = (low + high) // 2
            if can_pair(mid):
                high = mid
            else:
                low = mid + 1
        return low
```

## Complexity

| Time | Space |
| - | - |
| O(n log n + n log m) where m = max(nums) - min(nums) | O(n) for the sorted copy |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.