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.
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.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1090 <= p <= (nums.length)/2The primary idea of this approach is to first sort the array so that elements which are closer in value are positioned adjacent to each other. This can help in reducing the maximum difference within selected pairs when paired greedily. Once sorted, the greedy approach looks to form pairs consecutively and calculate the differences. By doing so repetitively and minimizing the maximum difference, an optimal solution is achieved.
This C code uses quicksort to sort the input array, followed by a binary search to find the minimal maximum difference possible. The binary search checks for possible maximum differences by counting how many pairs can be formed with that maximum difference.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n), dominated by the sorting step.
Space Complexity: O(1), if we ignore the space used by the sorting algorithm.
This approach also begins by sorting the input array, but tackles the problem by employing a min-heap (priority queue). The idea is to manage the smallest differences available and decide pairs greedily based on this. The heap helps efficiently remove and manage differences, ensuring that the maximum difference in the formed pairs remains minimal.
In this Python code, we sort the array and then push all possible adjacent differences into a min-heap. We then pop the smallest difference and form pairs while keeping track of used elements until the required number of pairs is formed.
Java
Time Complexity: O(n log n) due to the heap operations.
Space Complexity: O(n) for holding the differences.
| Approach | Complexity |
|---|---|
| Approach 1: Sorting with Greedy Pairing | Time Complexity: O(n log n), dominated by the sorting step. |
| Approach 2: Priority Queue to Manage Differences | Time Complexity: O(n log n) due to the heap operations. |
Minimize the Maximum Difference of Pairs - Leetcode 2616 - Python • NeetCodeIO • 17,720 views views
Watch 9 more video solutions →Practice Minimize the Maximum Difference of Pairs with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor