LeetCode 1906: Minimum Absolute Difference Queries Solution

Master LeetCode problem 1906 (Minimum Absolute Difference Queries), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1906. Minimum Absolute Difference Queries

Problem Explanation

Explanation

To solve this problem, we can precompute the minimum absolute difference for all subarrays of the input array nums. Then, for each query [li, ri], we can directly look up the precomputed minimum absolute difference for the subarray nums[li...ri].

Algorithm:

  1. Create a prefix array prefix where prefix[i] stores the minimum absolute difference within the subarray nums[0...i].
  2. Initialize a map lastIndex where lastIndex[nums[i]] stores the last index of nums[i] encountered in the array.
  3. Initialize a result array ans to store the answers for each query.
  4. Iterate over the input array nums to compute the values for prefix and lastIndex.
  5. Iterate over each query [li, ri]:
    • If all elements in the subarray are the same, set the result as -1.
    • Otherwise, find the minimum absolute difference using prefix[ri] - prefix[li].

Time Complexity:

  • Building the prefix array and lastIndex map takes O(n) time.
  • Answering each query takes O(1) time.
  • Overall time complexity is O(n + q), where n is the length of the input array nums and q is the number of queries.

Space Complexity:

  • Additional space for prefix and lastIndex is O(n).
  • Space complexity is O(n) due to the prefix array and lastIndex map.

Solution Code

import java.util.*;

class Solution {
    public int[] minDifference(int[] nums, int[][] queries) {
        int n = nums.length;
        int[] prefix = new int[n];
        Map<Integer, Integer> lastIndex = new HashMap<>();
        
        for (int i = 0; i < n; i++) {
            prefix[i] = lastIndex.containsKey(nums[i]) ? Math.min(Math.abs(i - lastIndex.get(nums[i])), prefix[i - 1]) : (i > 0 ? prefix[i - 1] : Integer.MAX_VALUE);
            lastIndex.put(nums[i], i);
        }
        
        int[] ans = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int li = queries[i][0];
            int ri = queries[i][1];
            if (li < ri && lastIndex.get(nums[li]) < ri) {
                ans[i] = -1;
            } else {
                ans[i] = prefix[ri] - (li > 0 ? prefix[li - 1] : 0);
            }
        }
        
        return ans;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1906 (Minimum Absolute Difference Queries)?

This page provides optimized solutions for LeetCode problem 1906 (Minimum Absolute Difference Queries) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1906 (Minimum Absolute Difference Queries)?

The time complexity for LeetCode 1906 (Minimum Absolute Difference Queries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1906 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1906 in Java, C++, or Python.

Back to LeetCode Solutions