Tricky Solution with Binary Search + HashMap
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 []
class Solution {
    private int binarySearch(List<Integer> indices, int index) {
        int left = 0, right = indices.size() - 1;
        int mid = -1;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (indices.get(mid) == index) {
                break;
            } else if (indices.get(mid) > index) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return mid;
    }

    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int qsize = queries.length;
        int nsize = nums.length;
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < nsize; i++) {
            map.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
        }
        List<Integer> result = new ArrayList<>(Collections.nCopies(qsize, -1));
        for (int i = 0; i < qsize; i++) {
            int index = queries[i];
            int val = nums[index];
            if (map.get(val).size() == 1) {
                continue;
            }
            List<Integer> indices = map.get(val);
            int pos = binarySearch(indices, index);
            int size = indices.size();
            int right = (pos + 1) % size;
            int left = (pos - 1 + size) % size;
            int disRight = Math.abs(index - indices.get(right));
            int disLeft = Math.abs(index - indices.get(left));
            disRight = Math.min(disRight, nsize - disRight);
            disLeft = Math.min(disLeft, nsize - disLeft);
            result.set(i, Math.min(disRight, disLeft));
        }
        return result;
    }
}
```
['Hash Table', 'Binary Search', 'Java']