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
.
[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 subarray nums[li...ri]
containing the elements of nums
between the 0-based indices li
and ri
(inclusive).
Return an array ans
where ans[i]
is the answer to the ith
query.
A subarray is a contiguous sequence of elements in an array.
The value of |x|
is defined as:
x
if x >= 0
.-x
if x < 0
.
Example 1:
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.
Example 2:
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.
Constraints:
2 <= nums.length <= 105
1 <= nums[i] <= 100
1 <= queries.length <= 2 * 104
0 <= li < ri < nums.length
This problem asks to find the minimum absolute difference between any two distinct elements within given subarrays of an input array. The solution uses a prefix sum approach for efficient query processing.
Approach:
Prefix Sum: A 2D prefix sum array preSum
is created. preSum[i][j]
stores the count of occurrences of the number j
in the subarray nums[0...i-1]
. This allows for quick calculation of the number of occurrences of each element within any subarray nums[l...r]
using preSum[r+1][j] - preSum[l][j]
.
Query Processing: For each query [l, r]
, we iterate through the possible numbers (1 to 100, as specified in the constraints).
j
is present in the subarray nums[l...r]
using the prefix sum.last
), we calculate the absolute difference between j
and last
, updating the minimum difference t
if necessary.t
will remain at its initial high value (infinity in Python, Integer.MAX_VALUE
in Java, etc.), resulting in the output -1 after the loop.Result: The minimum absolute difference for each query is stored in the ans
array and returned.
Time Complexity Analysis:
Prefix Sum Calculation: The prefix sum array is computed in O(m*100) time, where 'm' is the length of the input array nums
. This is because we iterate through the array once for each possible number (1 to 100).
Query Processing: For each query, we iterate through at most 100 possible numbers (1 to 100). Thus, processing n
queries takes O(n*100) time.
Therefore, the overall time complexity is O(m100 + n100), which simplifies to O(m + n) since 100 is a constant.
Space Complexity Analysis:
The space complexity is dominated by the prefix sum array preSum
, which has dimensions (m+1) x 101. Therefore, the space complexity is O(m).
Code Explanation (Python3):
class Solution:
def minDifference(self, nums: List[int], queries: List[List[int]]) -> List[int]:
m, n = len(nums), len(queries)
pre_sum = [[0] * 101 for _ in range(m + 1)] # Initialize prefix sum array
for i in range(1, m + 1):
for j in range(1, 101):
t = 1 if nums[i - 1] == j else 0 # Count occurrences of j
pre_sum[i][j] = pre_sum[i - 1][j] + t
ans = []
for i in range(n):
left, right = queries[i][0], queries[i][1] + 1 #Extract query indices
t = float('inf') # Initialize minimum difference to infinity
last = -1
for j in range(1, 101):
if pre_sum[right][j] - pre_sum[left][j] > 0: #Check if j is present in the subarray
if last != -1:
t = min(t, j - last) #Update minimum difference
last = j
if t == float('inf'): #Handle case where all elements are the same
t = -1
ans.append(t)
return ans
The code in other languages (Java, C++, Go, TypeScript) follows the same algorithmic approach, with minor syntax differences. The core logic of prefix sum calculation and query processing remains consistent.