Minimum Absolute Difference Between Elements With Constraint

You are given a 0-indexed integer array nums and an integer x.

Find the minimum absolute difference between two elements in the array that are at least x indices apart.

In other words, find two indices i and j such that abs(i - j) >= x and abs(nums[i] - nums[j]) is minimized.

Return an integer denoting the minimum absolute difference between two elements that are at least x indices apart.

Example 1
Inputnums = [4,3,2,4], x = 2
Output0
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.
Example 2
Inputnums = [5,3,2,10,15], x = 1
Output1
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.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 0 <= x < nums.length

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate