3488. Closest Equal Element Queries Medium

@problem@discussion
#Array#Hash Table#Binary Search



1/**
2 * [3488] Closest Equal Element Queries
3 *
4 * You are given a circular array nums and an array queries.
5 * For each query i, you have to find the following:
6 * 
7 * 	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.
8 * 
9 * Return an array answer of the same size as queries, where answer[i] represents the result for query i.
10 *  
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">nums = [1,3,1,4,1,3,2], queries = [0,3,5]</span>
14 * Output: <span class="example-io">[2,-1,3]</span>
15 * Explanation:
16 * 
17 * 	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.
18 * 	Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.
19 * 	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).
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [1,2,3,4], queries = [0,1,2,3]</span>
24 * Output: <span class="example-io">[-1,-1,-1,-1]</span>
25 * Explanation:
26 * Each value in nums is unique, so no index shares the same value as the queried element. This results in -1 for all queries.
27 * </div>
28 *  
29 * Constraints:
30 * 
31 * 	1 <= queries.length <= nums.length <= 10^5
32 * 	1 <= nums[i] <= 10^6
33 * 	0 <= queries[i] < nums.length
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/closest-equal-element-queries/
39// discuss: https://leetcode.com/problems/closest-equal-element-queries/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn solve_queries(nums: Vec<i32>, queries: Vec<i32>) -> Vec<i32> {
45        vec![]
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3488() {
57    }
58}
59

Back
© 2026 bowen.ge All Rights Reserved.