2817. Minimum Absolute Difference Between Elements With Constraint Medium
1/**
2 * [2817] Minimum Absolute Difference Between Elements With Constraint
3 *
4 * You are given a 0-indexed integer array nums and an integer x.
5 * Find the minimum absolute difference between two elements in the array that are at least x indices apart.
6 * In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized.
7 * Return an integer denoting the minimum absolute difference between two elements that are at least x indices apart.
8 *
9 * <strong class="example">Example 1:
10 *
11 * Input: nums = [4,3,2,4], x = 2
12 * Output: 0
13 * Explanation: We can select nums[0] = 4 and nums[3] = 4.
14 * They are at least 2 indices apart, and their absolute difference is the minimum, 0.
15 * It can be shown that 0 is the optimal answer.
16 *
17 * <strong class="example">Example 2:
18 *
19 * Input: nums = [5,3,2,10,15], x = 1
20 * Output: 1
21 * Explanation: We can select nums[1] = 3 and nums[2] = 2.
22 * They are at least 1 index apart, and their absolute difference is the minimum, 1.
23 * It can be shown that 1 is the optimal answer.
24 *
25 * <strong class="example">Example 3:
26 *
27 * Input: nums = [1,2,3,4], x = 3
28 * Output: 3
29 * Explanation: We can select nums[0] = 1 and nums[3] = 4.
30 * They are at least 3 indices apart, and their absolute difference is the minimum, 3.
31 * It can be shown that 3 is the optimal answer.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= nums.length <= 10^5
37 * 1 <= nums[i] <= 10^9
38 * 0 <= x < nums.length
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/minimum-absolute-difference-between-elements-with-constraint/
44// discuss: https://leetcode.com/problems/minimum-absolute-difference-between-elements-with-constraint/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn min_absolute_difference(nums: Vec<i32>, x: i32) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_2817() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.