binary search + hashmap solution (beats 80% runtime)
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
import java.util.*;

class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int n = nums.length;
        Map<Integer, List<Integer>> map = new HashMap<>();

        for (int i = 0; i < n; i++) {
            map.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
        }

        List<Integer> result = new ArrayList<>();

        for (int q : queries) {
            int val = nums[q];
            List<Integer> indices = map.get(val);

            if (indices.size() == 1) {
                result.add(-1);
                continue;
            }

            int left = 0, right = indices.size() - 1, mid = 0;
            while (left <= right) {
                mid = (left + right) / 2;
                if (indices.get(mid) == q) break;
                else if (indices.get(mid) < q) left = mid + 1;
                else right = mid - 1;
            }

            int len = indices.size();
            int prev = indices.get((mid - 1 + len) % len);
            int after = indices.get((mid + 1) % len);

            int d1 = (q - prev + n) % n;
            int d2 = (after - q + n) % n;

            result.add(Math.min(d1, d2));
        }

        return result;
    }
}

```
['Java']