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:
Input: nums = [4,3,2,4], x = 2
Output: 0
Explanation: 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 0 is the optimal answer.
Example 2:
Input: nums = [5,3,2,10,15], x = 1
Output: 1
Explanation: 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 1 is the optimal answer.
Example 3:
Input: nums = [1,2,3,4], x = 3
Output: 3
Explanation: 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 3 is the optimal answer.
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
0 <= x < nums.length
The most straightforward approach is to iterate through all possible pairs of elements in the array and check if their indices are at least x
apart. If they are, we calculate the absolute difference between the elements and update the minimum difference found so far.
def min_abs_difference_brute_force(nums, x):
n = len(nums)
min_diff = float('inf')
for i in range(n):
for j in range(n):
if abs(i - j) >= x:
min_diff = min(min_diff, abs(nums[i] - nums[j]))
return min_diff
The time complexity of this brute-force approach is O(n^2), where n is the length of the nums
array. This is because we have nested loops that iterate through all possible pairs of elements.
The space complexity is O(1) because we are only using a constant amount of extra space to store the minimum difference.
To improve the time complexity, we can use a sorted list to keep track of the elements within the range [i+x, n-1]
. For each element nums[i]
, we want to find the element in the sorted list that is closest to nums[i]
. We can efficiently find this closest element using binary search.
min_diff
to infinity.i = 0
to n - x - 1
.i
, maintain a sorted list of nums[j]
for j >= i + x
.nums[i]
.min_diff
with the minimum absolute difference found.import bisect
def min_abs_difference_optimal(nums, x):
n = len(nums)
min_diff = float('inf')
sorted_list = []
for i in range(n - x):
# Ensure sorted_list is populated for binary search
if i == 0:
for j in range(x, n):
bisect.insort(sorted_list, nums[j])
# Find closest element to nums[i] in sorted_list
idx = bisect.bisect_left(sorted_list, nums[i])
if idx > 0:
min_diff = min(min_diff, abs(nums[i] - sorted_list[idx - 1]))
if idx < len(sorted_list):
min_diff = min(min_diff, abs(nums[i] - sorted_list[idx]))
# Remove nums[i+x] as it is outside the valid range for the next iteration
sorted_list.remove(nums[i + x])
return min_diff
The time complexity of this approach is O(n log n), where n is the length of the nums
array. The bisect.insort
and bisect.bisect_left
operations each take O(log n) time, and we perform these operations n times in the loop. Removing an element from list also take O(n).
The space complexity is O(n), where n is the length of the nums
array. This is because we are storing elements in a sorted list, which can contain at most n - x
elements, but in the worst case it is O(n).
x
is 0, any two elements can be compared. x
is greater than or equal to the length of the array, there are no valid pairs.