The minimum absolute difference of an array a is defined as the
minimum value of |a[i] - a[j]|, where 0 <= i < j < a.length and a[i] != a[j]. If all elements of a are the same , the minimum absolute difference is -1.
For example, the minimum absolute difference of the array [5,_2_ ,_3_ ,7,2] is |2 - 3| = 1. Note that it is not 0 because a[i] and a[j] must be different.
You are given an integer array nums and the array queries where
queries[i] = [li, ri]. For each query i, compute the minimum absolute difference of the subarraynums[li...ri] containing the elements of
nums between the 0-based indices li and ri (inclusive).
Return _anarray _answhereans[i]is the answer to theithquery.
A subarray is a contiguous sequence of elements in an array.
Input: nums =[1,3,4,8], queries =[[0,1],[1,2],[2,3],[0,3]]Output: [2,1,4,1]Explanation: The queries are processed as follows:- queries[0]=[0,1]: The subarray is[_1_ ,_3_] and the minimum absolute difference is|1-3|=2.- queries[1]=[1,2]: The subarray is[_3_ ,_4_] and the minimum absolute difference is|3-4|=1.- queries[2]=[2,3]: The subarray is[_4_ ,_8_] and the minimum absolute difference is|4-8|=4.- queries[3]=[0,3]: The subarray is[1,_3_ ,_4_ ,8] and the minimum absolute difference is|3-4|=1.
Input: nums =[4,5,2,2,7,10], queries =[[2,3],[0,2],[0,5],[3,5]]Output: [-1,1,1,3]Explanation: The queries are processed as follows:- queries[0]=[2,3]: The subarray is[2,2] and the minimum absolute difference is-1 because all the
elements are the same.- queries[1]=[0,2]: The subarray is[_4_ ,_5_ ,2] and the minimum absolute difference is|4-5|=1.- queries[2]=[0,5]: The subarray is[_4_ ,_5_ ,2,2,7,10] and the minimum absolute difference is|4-5|=1.- queries[3]=[3,5]: The subarray is[2,_7_ ,_10_] and the minimum absolute difference is|7-10|=3.
For each query, we need to find the minimum absolute difference in a subarray. Since the range of values is small (1 ≤ nums[i] ≤ 10^5), we can use prefix frequency arrays and process queries offline. For each query, collect all unique values in the range, sort them, and compute the minimum difference between consecutive values.
classSolution {
funminDifference(nums: IntArray, queries: Array<IntArray>): IntArray {
val n = nums.size
val freq = Array(n+1) { IntArray(100001) }
for (i in0 until n) {
for (v in1..100000) freq[i+1][v] = freq[i][v]
freq[i+1][nums[i]]++ }
val ans = IntArray(queries.size)
for ((qi, q) in queries.withIndex()) {
val(l, r) = q[0] to q[1]+1val vals = mutableListOf<Int>()
for (v in1..100000) {
if (freq[r][v] - freq[l][v] > 0) vals.add(v)
}
if (vals.size < 2) ans[qi] = -1else {
var minDiff = Int.MAX_VALUE
for (i in1 until vals.size)
minDiff = minOf(minDiff, vals[i] - vals[i-1])
ans[qi] = minDiff
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defmin_difference(nums: list[int], queries: list[list[int]]) -> list[int]:
n = len(nums)
freq = [[0]*100001for _ in range(n+1)]
for i in range(n):
for v in range(1, 100001):
freq[i+1][v] = freq[i][v]
freq[i+1][nums[i]] +=1 ans = []
for l, r in queries:
vals = [v for v in range(1, 100001) if freq[r+1][v] - freq[l][v] >0]
if len(vals) <2:
ans.append(-1)
else:
min_diff = min(vals[i] - vals[i-1] for i in range(1, len(vals)))
ans.append(min_diff)
return ans
impl Solution {
pubfnmin_difference(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let n = nums.len();
letmut freq =vec![vec![0; 100001]; n+1];
for i in0..n {
for v in1..=100000 {
freq[i+1][v] = freq[i][v];
}
freq[i+1][nums[i] asusize] +=1;
}
letmut ans =vec![];
for q in queries.iter() {
let l = q[0] asusize;
let r = q[1] asusize+1;
letmut vals =vec![];
for v in1..=100000 {
if freq[r][v] - freq[l][v] >0 {
vals.push(v asi32);
}
}
if vals.len() <2 {
ans.push(-1);
} else {
letmut min_diff =i32::MAX;
for i in1..vals.len() {
min_diff = min_diff.min(vals[i] - vals[i-1]);
}
ans.push(min_diff);
}
}
ans
}
}