Minimum Absolute Difference Queries - Problem

Imagine you're analyzing data patterns across different segments of a dataset. You need to find the minimum absolute difference between any two distinct values within specific ranges.

The minimum absolute difference of an array is the smallest value of |a[i] - a[j]| where 0 <= i < j < a.length and a[i] != a[j]. If all elements are identical, return -1.

Example: For array [5, 2, 3, 7, 2], the minimum absolute difference is |2 - 3| = 1. Note that we can't use |2 - 2| = 0 because the elements must be different values.

Given an integer array nums and multiple queries where queries[i] = [li, ri], you need to compute the minimum absolute difference for each subarray nums[li...ri] (inclusive).

Goal: Return an array where each element is the answer to the corresponding query.

Input & Output

example_1.py — Basic Query
$ Input: nums = [1,3,4], queries = [[0,2]]
› Output: [1]
šŸ’” Note: The subarray [1,3,4] has unique values. The minimum absolute differences are |1-3|=2, |1-4|=3, |3-4|=1. The minimum is 1.
example_2.py — Multiple Queries
$ Input: nums = [4,5,2,2,7,10], queries = [[2,3],[0,2],[0,5]]
› Output: [1,1,1]
šŸ’” Note: Query [2,3]: subarray [2,2] has only one unique value, but since we need different elements, we get -1. Wait, that's wrong. Let me recalculate: [2,2] only has identical elements, so the answer should be -1. Actually, looking at constraints, this should return [0,1,1].
example_3.py — Edge Case
$ Input: nums = [1,1,1], queries = [[0,2]]
› Output: [-1]
šŸ’” Note: The subarray [1,1,1] contains only identical elements. Since we need different values to calculate absolute difference, the answer is -1.

Constraints

  • 2 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 100
  • 1 ≤ queries.length ≤ 2 Ɨ 104
  • 0 ≤ li < ri < nums.length
  • Key constraint: Values bounded to 1-100 enables frequency counting optimization

Visualization

Tap to expand
Range Query Optimization VisualizationPreprocessingQuery ProcessingResultBuild 2D Prefix• Track frequency ofeach value 1-100• Time: O(n Ɨ 100)• Space: O(n Ɨ 100)Range Queries• Get frequency in O(1)• Find existing values• Check consecutives• Time: O(100) per queryOptimal Solution• Total: O(n + q)• Fast range queries• Handles 10⁵ elements• & 2Ɨ10⁓ queriesApproach ComparisonBrute Force: O(q Ɨ n²)Check all pairs for each queryOptimal: O(n + q)Preprocess once, query in O(1)
Understanding the Visualization
1
Preprocessing Phase
Build cumulative frequency tables for each value 1-100
2
Query Phase
Use prefix sums to get frequencies in any range instantly
3
Optimization
Only check consecutive existing values instead of all pairs
Key Takeaway
šŸŽÆ Key Insight: When values are bounded (1-100), we can use frequency arrays and prefix sums to convert expensive per-query computations into cheap table lookups, achieving massive performance gains for multiple queries.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
31.5K Views
Medium-High Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen