Input: nums =[4,3,2,4], x =2Output: 0Explanation: We can select nums[0]=4 and nums[3]=4.They are at least 2 indices apart, and their absolute difference is the minimum,0.It can be shown that 0is the optimal answer.
Input: nums =[5,3,2,10,15], x =1Output: 1Explanation: We can select nums[1]=3 and nums[2]=2.They are at least 1 index apart, and their absolute difference is the minimum,1.It can be shown that 1is the optimal answer.
Input: nums =[1,2,3,4], x =3Output: 3Explanation: We can select nums[0]=1 and nums[3]=4.They are at least 3 indices apart, and their absolute difference is the minimum,3.It can be shown that 3is the optimal answer.
To find the minimum absolute difference between elements at least x indices apart, we can use an ordered set to keep track of elements that are at least x indices behind the current index. For each index, we check the closest values in the set to minimize the absolute difference.
classSolution {
public:int minAbsoluteDifference(vector<int>& nums, int x) {
set<int> st;
int ans = INT_MAX;
for (int i = x; i < nums.size(); ++i) {
st.insert(nums[i-x]);
auto it = st.lower_bound(nums[i]);
if (it != st.end()) ans = min(ans, abs(*it - nums[i]));
if (it != st.begin()) {
--it;
ans = min(ans, abs(*it - nums[i]));
}
}
return ans;
}
};
funcminAbsoluteDifference(nums []int, xint) int {
import"sort"st:= []int{}
ans:=1<<30fori:=x; i < len(nums); i++ {
st = append(st, nums[i-x])
sort.Ints(st)
idx:=sort.SearchInts(st, nums[i])
ifidx < len(st) {
ifabs(st[idx]-nums[i]) < ans {
ans = abs(st[idx]-nums[i])
}
}
ifidx > 0 {
ifabs(st[idx-1]-nums[i]) < ans {
ans = abs(st[idx-1]-nums[i])
}
}
}
returnans}
funcabs(xint) int { ifx < 0 { return-x }; returnx }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicintminAbsoluteDifference(int[] nums, int x) {
TreeSet<Integer> st =new TreeSet<>();
int ans = Integer.MAX_VALUE;
for (int i = x; i < nums.length; ++i) {
st.add(nums[i-x]);
Integer hi = st.ceiling(nums[i]);
Integer lo = st.floor(nums[i]);
if (hi !=null) ans = Math.min(ans, Math.abs(hi - nums[i]));
if (lo !=null) ans = Math.min(ans, Math.abs(lo - nums[i]));
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funminAbsoluteDifference(nums: IntArray, x: Int): Int {
val st = sortedSetOf<Int>()
var ans = Int.MAX_VALUE
for (i in x until nums.size) {
st.add(nums[i-x])
val hi = st.ceiling(nums[i])
val lo = st.floor(nums[i])
if (hi !=null) ans = minOf(ans, abs(hi - nums[i]))
if (lo !=null) ans = minOf(ans, abs(lo - nums[i]))
}
return ans
}
funabs(a: Int) = if (a < 0) -a else a
}
1
2
3
4
5
6
7
8
9
10
11
12
defmin_absolute_difference(nums: list[int], x: int) -> int:
import bisect
st: list[int] = []
ans = float('inf')
for i in range(x, len(nums)):
bisect.insort(st, nums[i-x])
idx = bisect.bisect_left(st, nums[i])
if idx < len(st):
ans = min(ans, abs(st[idx] - nums[i]))
if idx >0:
ans = min(ans, abs(st[idx-1] - nums[i]))
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnmin_absolute_difference(nums: Vec<i32>, x: i32) -> i32 {
use std::collections::BTreeSet;
letmut st = BTreeSet::new();
letmut ans =i32::MAX;
for i in x asusize..nums.len() {
st.insert(nums[i-x asusize]);
iflet Some(&hi) = st.range(nums[i]..).next() {
ans = ans.min((hi - nums[i]).abs());
}
iflet Some(&lo) = st.range(..=nums[i]).next_back() {
ans = ans.min((lo - nums[i]).abs());
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
minAbsoluteDifference(nums: number[], x: number):number {
constst: number[] = [];
letans= Number.MAX_SAFE_INTEGER;
for (leti=x; i<nums.length; ++i) {
st.push(nums[i-x]);
st.sort((a, b) =>a-b);
constidx=st.findIndex(v=>v>=nums[i]);
if (idx!==-1) ans= Math.min(ans, Math.abs(st[idx] -nums[i]));
if (idx>0) ans= Math.min(ans, Math.abs(st[idx-1] -nums[i]));
}
returnans;
}
}