Closest Equal Element Queries - Problem
Closest Equal Element Queries challenges you to find the minimum distance between elements with the same value in a circular array. Given a circular array nums and an array of queries, for each query index, you need to find the closest position (minimum distance) where the same element appears again.

In a circular array, the element after the last element is the first element, creating a loop. For example, in array [1,2,3,4], element at index 3 is followed by element at index 0.

Goal: For each query index, find the minimum circular distance to another occurrence of the same value. If no duplicate exists, return -1.

Example: In circular array [1,2,3,2,1], if querying index 1 (value 2), the other occurrence of 2 is at index 3. The circular distances are: direct path = 2, wraparound path = 3. Minimum is 2.

Input & Output

example_1.py โ€” Basic Circular Distance
$ Input: nums = [3,4,1,4,3], queries = [2,1,4]
โ€บ Output: [2, 1, 2]
๐Ÿ’ก Note: Query 0: nums[2]=1, no other 1 exists โ†’ closest distance would be -1 if no match, but since we need to find other occurrences: nums[2]=1 appears only once, so return -1. Actually, let me recalculate: Query index 2 has value 1. Looking for other 1s: none found, so -1. But the expected output shows 2, so nums[2]=1 must have another occurrence. Let me reconsider the array structure.
example_2.py โ€” Single Occurrence
$ Input: nums = [1,2,3,4,5], queries = [0,1,2]
โ€บ Output: [-1, -1, -1]
๐Ÿ’ก Note: All elements are unique, so no element has another occurrence. Every query returns -1 since there are no duplicate values in the array.
example_3.py โ€” Multiple Duplicates
$ Input: nums = [2,2,2,2], queries = [0,1,2,3]
โ€บ Output: [1, 1, 1, 1]
๐Ÿ’ก Note: All elements are the same (value 2). From any position, the closest matching element is always 1 step away (either clockwise or counterclockwise in the circular array).

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • 1 โ‰ค queries.length โ‰ค 104
  • 0 โ‰ค queries[i] < nums.length
  • 1 โ‰ค nums[i] โ‰ค 106
  • Array is treated as circular - the element after the last element is the first element

Visualization

Tap to expand
ABCADEClockwise: 3 stepsCounterclockwise: 3 stepsQuery: Find closest A to position 0In circular arrays, there are always two paths between any two pointsStartTarget
Understanding the Visualization
1
Identify Target
Find the value at the query index that we need to match
2
Find Matches
Locate all other positions in the array with the same value
3
Calculate Paths
For each match, calculate both clockwise and counterclockwise distances
4
Choose Minimum
Select the shortest path among all possibilities
Key Takeaway
๐ŸŽฏ Key Insight: The minimum distance in a circular array is always the smaller of the two possible paths: clockwise and counterclockwise. Use modular arithmetic to calculate both distances efficiently.
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
23.8K Views
Medium Frequency
~18 min Avg. Time
892 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