Minimum Threshold for Inversion Pairs Count Problem
Description
LeetCode Problem 3520.
You are given an array of integers nums and an integer k.
An inversion pair with a threshold x is defined as a pair of indices (i, j) such that:
i < jnums[i] > nums[j]- The difference between the two numbers is at most
x(i.e.nums[i] - nums[j] <= x).
Your task is to determine the minimum integer min_threshold such that there are at least k inversion pairs with threshold min_threshold.
If no such integer exists, return -1.
Example 1:
Input: nums = [1,2,3,4,3,2,1], k = 7
Output: 2
Explanation:
For threshold x = 2, the pairs are:
1. (3, 4) where nums[3] == 4 and nums[4] == 3.
2. (2, 5) where nums[2] == 3 and nums[5] == 2.
3. (3, 5) where nums[3] == 4 and nums[5] == 2.
4. (4, 5) where nums[4] == 3 and nums[5] == 2.
5. (1, 6) where nums[1] == 2 and nums[6] == 1.
6. (2, 6) where nums[2] == 3 and nums[6] == 1.
7. (4, 6) where nums[4] == 3 and nums[6] == 1.
8. (5, 6) where nums[5] == 2 and nums[6] == 1.
There are less than k inversion pairs if we choose any integer less than 2 as threshold.
Example 2:
Input: nums = [10,9,9,9,1], k = 4
Output: 8
Explanation:
For threshold x = 8, the pairs are:
1. (0, 1) where nums[0] == 10 and nums[1] == 9.
2. (0, 2) where nums[0] == 10 and nums[2] == 9.
3. (0, 3) where nums[0] == 10 and nums[3] == 9.
4. (1, 4) where nums[1] == 9 and nums[4] == 1.
5. (2, 4) where nums[2] == 9 and nums[4] == 1.
6. (3, 4) where nums[3] == 9 and nums[4] == 1.
There are less than k inversion pairs if we choose any integer less than 8 as threshold.
Constraints:
1 <= nums.length <= 10^41 <= nums[i] <= 10^91 <= k <= 10^9
Solution
Approach: Binary Search on the Threshold and a Fenwick Tree
The number of inversion pairs with threshold x can only grow when x grows, so we can binary search for the smallest x that reaches k pairs. The threshold ranges from 1 to max - min; if even the largest threshold gives fewer than k pairs, return -1.
To count the pairs for a given x, compress the values and scan the array from left to right with a Fenwick tree that holds the elements seen so far. For the current value v, the pairs it forms with earlier elements are the earlier elements in the range (v, v + x], which is a difference of two prefix queries. Then insert v into the tree.
Complexity: O(n log n log V) time and O(n) extra space, where V is the largest value.
from bisect import bisect_left, bisect_right
class Solution:
def minThreshold(self, nums: List[int], k: int) -> int:
vals = sorted(set(nums))
m = len(vals)
pos = [bisect_left(vals, v) + 1 for v in nums] # 1-based rank of each element
def count(x: int) -> int:
# pairs i < j with 0 < nums[i] - nums[j] <= x
tree = [0] * (m + 1)
total = 0
for j, v in enumerate(nums):
hi = bisect_right(vals, v + x)
for p, sign in ((hi, 1), (pos[j], -1)):
while p > 0:
total += sign * tree[p]
p -= p & -p
p = pos[j]
while p <= m:
tree[p] += 1
p += p & -p
return total
lo, hi = 1, vals[-1] - vals[0]
if hi < 1 or count(hi) < k:
return -1
while lo < hi:
mid = (lo + hi) // 2
if count(mid) >= k:
hi = mid
else:
lo = mid + 1
return lo