Closest Equal Element Queries

You are given a circular array nums and an array queries.

For each query i, find 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
Inputnums = [1,3,1,4,1,3,2], queries = [0,3,5]
Output[2,-1,3]
For queries 0, 3, and 5, the nearest equal elements have distances 2, none, and 3 respectively in the circular array.
Example 2
Inputnums = [1,2,3,4], queries = [0,1,2,3]
Output[-1,-1,-1,-1]
Each value in nums is unique, so no index shares the same value as the queried element, resulting in -1 for all queries.

Constraints

  • 1 <= queries.length <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6
  • 0 <= queries[i] < nums.length

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate