LeetCode 3488: Closest Equal Element Queries Solution

Master LeetCode problem 3488 (Closest Equal Element 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.

3488. Closest Equal Element Queries

Problem Explanation

Explanation

To solve this problem, we can iterate through the nums array once to build a mapping of each element to its indices. Then, for each query index queries[i], we can find the closest index with the same value by looking at the indices of that value in the mapping. We need to consider the circular nature of the array when calculating the minimum distance.

Here is the step-by-step algorithm:

  1. Create a HashMap to store the mapping of each value in nums to its indices.
  2. Iterate through nums to build this mapping.
  3. For each query index queries[i], find the indices of the value at that index in the mapping.
  4. Calculate the minimum circular distance to any of these indices.
  5. If no index with the same value is found, set the answer for that query to -1.

The time complexity of this algorithm is O(n) where n is the length of the nums array since we iterate through the array only once. The space complexity is also O(n) to store the mapping in the HashMap.

Solution Code

import java.util.*;

class Solution {
    public int[] closestEqualElement(int[] nums, int[] queries) {
        Map<Integer, List<Integer>> numToIndices = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!numToIndices.containsKey(nums[i])) {
                numToIndices.put(nums[i], new ArrayList<>());
            }
            numToIndices.get(nums[i]).add(i);
        }
        
        int[] answer = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int queryIndex = queries[i];
            int queryValue = nums[queryIndex];
            if (!numToIndices.containsKey(queryValue)) {
                answer[i] = -1;
            } else {
                List<Integer> indices = numToIndices.get(queryValue);
                int minDistance = Integer.MAX_VALUE;
                for (int idx : indices) {
                    int dist = Math.abs(queryIndex - idx);
                    dist = Math.min(dist, nums.length - dist); // considering circular distance
                    minDistance = Math.min(minDistance, dist);
                }
                answer[i] = minDistance;
            }
        }
        
        return answer;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3488 (Closest Equal Element Queries)?

This page provides optimized solutions for LeetCode problem 3488 (Closest Equal Element 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 3488 (Closest Equal Element Queries)?

The time complexity for LeetCode 3488 (Closest Equal Element Queries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3488 on DevExCode?

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

Back to LeetCode Solutions