Minimum Threshold for Inversion Pairs Count - Problem

You are tasked with analyzing inversion pairs in an array with a twist - we need to find pairs that are "close enough" in value!

Given an array of integers nums and an integer k, an inversion pair with threshold x is defined as a pair of indices (i, j) such that:

  • i < j (left element comes before right element)
  • nums[i] > nums[j] (left element is greater - this creates the inversion)
  • nums[i] - nums[j] ≤ x (the difference is at most the threshold)

Your goal is to find the minimum threshold such that there are at least k inversion pairs with that threshold. If it's impossible to find k such pairs, return -1.

Example: In array [4, 1, 3, 2] with threshold 2, pairs (0,1), (0,3), (2,3) are valid inversion pairs because 4>1 (diff=3≤2 ❌), 4>2 (diff=2≤2 ✓), 3>2 (diff=1≤2 ✓).

Input & Output

example_1.py — Python
$ Input: nums = [4, 1, 3, 2], k = 2
Output: 2
💡 Note: With threshold 2: pairs (0,3) where 4>2 and 4-2=2≤2, and (2,3) where 3>2 and 3-2=1≤2. Total: 2 pairs ≥ k=2.
example_2.py — Python
$ Input: nums = [1, 2, 3, 4], k = 1
Output: -1
💡 Note: Array is already sorted, no inversion pairs exist (i < j but nums[i] ≤ nums[j] for all valid i,j).
example_3.py — Python
$ Input: nums = [5, 1, 4, 2, 3], k = 3
Output: 2
💡 Note: With threshold 2: pairs (0,1) 5>1 diff=4>2❌, (0,3) 5>2 diff=3>2❌, (0,4) 5>3 diff=2≤2✓, (2,3) 4>2 diff=2≤2✓, (2,4) 4>3 diff=1≤2✓. Total: 3 pairs ≥ k=3.

Visualization

Tap to expand
🎯 Teacher's Grade Comparison SystemAlice85Bob72Carol78Dave70Step 1: Find all inversions (earlier > later)85-72=1385-70=1578-70=8Step 2: Apply threshold filterThreshold = 10 (tolerance level)• Alice > Bob: 85-72=13 > 10 ❌• Alice > Dave: 85-70=15 > 10 ❌• Carol > Dave: 78-70=8 ≤ 10 ✓Binary Search ProcessRange: [0, 15] (max difference)Try threshold = 7: count = 0 < k=2Try threshold = 11: count = 1 < k=2Try threshold = 13: count = 2 ≥ k=2 ✓Try threshold = 12: count = 1 < k=2Minimum threshold: 13
Understanding the Visualization
1
Identify Inversions
Find all pairs where earlier score > later score
2
Apply Threshold
Only count pairs where difference ≤ threshold
3
Binary Search
Find minimum threshold that gives ≥k pairs
4
Optimize Counting
Use merge sort to count pairs efficiently
Key Takeaway
🎯 Key Insight: Binary search works because if threshold x allows ≥k pairs, any larger threshold will also work. We efficiently count pairs for each candidate threshold to find the minimum viable one.

Time & Space Complexity

Time Complexity
⏱️
O(n³)

O(n²) to find all possible thresholds, then O(n²) for each threshold to count pairs

n
2n
Quadratic Growth
Space Complexity
O(n²)

Store all possible threshold values (up to n² differences)

n
2n
Quadratic Space

Constraints

  • 2 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ k ≤ n(n-1)/2
  • All elements in nums are distinct
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
42.0K Views
Medium Frequency
~35 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen