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
In a circular array, the element after the last element is the first element, creating a loop. For example, in array
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
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code