3488. Closest Equal Element Queries LeetCode Solution

In this guide, you will get 3488. Closest Equal Element Queries LeetCode Solution with the best time and space complexity. The solution to Closest Equal Element Queries problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Closest Equal Element Queries solution in C++
  4. Closest Equal Element Queries solution in Java
  5. Closest Equal Element Queries solution in Python
  6. Additional Resources
3488. Closest Equal Element Queries LeetCode Solution image

Problem Statement of Closest Equal Element Queries

You are given a circular array nums and an array queries.
For each query i, you have to find the following:

The minimum distance between the element at index queries[i] and any other index j in the circular array, where nums[j] == nums[queries[i]]. If no such index exists, the answer for that query should be -1.

Return an array answer of the same size as queries, where answer[i] represents the result for query i.

Example 1:

Input: nums = [1,3,1,4,1,3,2], queries = [0,3,5]
Output: [2,-1,3]
Explanation:

Query 0: The element at queries[0] = 0 is nums[0] = 1. The nearest index with the same value is 2, and the distance between them is 2.
Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.
Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1).

Example 2:

Input: nums = [1,2,3,4], queries = [0,1,2,3]
Output: [-1,-1,-1,-1]
Explanation:
Each value in nums is unique, so no index shares the same value as the queried element. This results in -1 for all queries.

Constraints:

1 <= queries.length <= nums.length <= 105
1 <= nums[i] <= 106
0 <= queries[i] < nums.length

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3488. Closest Equal Element Queries LeetCode Solution in C++

class Solution {
 public:
  vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
    const int n = nums.size();
    vector<int> ans;
    // minDist[i] := the minimum distance between nums[i], and any other index j
    // in the circular array, where nums[j] == nums[i]
    vector<int> minDist(n, n);
    unordered_map<int, int> lastSeen;

    for (int i = 0; i < n * 2; ++i) {
      const int index = i % n;
      const int num = nums[index];
      if (const auto it = lastSeen.find(num); it != lastSeen.cend()) {
        const int prevIndex = it->second % n;
        const int d = i - prevIndex;
        minDist[index] = min(minDist[index], d);
        minDist[prevIndex] = min(minDist[prevIndex], d);
      }
      lastSeen[num] = i;
    }

    for (const int query : queries)
      ans.push_back(minDist[query] == n ? -1 : minDist[query]);

    return ans;
  }
};
/* code provided by PROGIEZ */

3488. Closest Equal Element Queries LeetCode Solution in Java

class Solution {
  public List<Integer> solveQueries(int[] nums, int[] queries) {
    final int n = nums.length;
    List<Integer> ans = new ArrayList<>();
    // minDist[i] := the minimum distance between nums[i], and any other index j in the circular
    // array, where nums[j] == nums[i]
    int[] minDist = new int[n];
    Arrays.fill(minDist, n);
    Map<Integer, Integer> lastSeen = new HashMap<>();

    for (int i = 0; i < n * 2; ++i) {
      final int index = i % n;
      final int num = nums[index];
      if (lastSeen.containsKey(num)) {
        final int prevIndex = lastSeen.get(num) % n;
        final int d = i - prevIndex;
        minDist[index] = Math.min(minDist[index], d);
        minDist[prevIndex] = Math.min(minDist[prevIndex], d);
      }
      lastSeen.put(num, i);
    }

    for (final int query : queries)
      ans.add(minDist[query] == n ? -1 : minDist[query]);

    return ans;
  }
}
// code provided by PROGIEZ

3488. Closest Equal Element Queries LeetCode Solution in Python

class Solution:
  def solveQueries(self, nums: list[int], queries: list[int]) -> list[int]:
    n = len(nums)
    # minDist[i] := the minimum distance between nums[i], and any other index j
    # in the circular array, where nums[j] == nums[i]
    minDist = [n] * n
    lastSeen = {}

    for i in range(n * 2):
      index = i % n
      num = nums[index]
      if num in lastSeen:
        prevIndex = lastSeen[num] % n
        d = i - prevIndex
        minDist[index] = min(minDist[index], d)
        minDist[prevIndex] = min(minDist[prevIndex], d)
      lastSeen[num] = i

    return [-1 if minDist[query] == n
            else minDist[query]
            for query in queries]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.