simple java solution || beats 100% || just map 
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 {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        HashMap<Integer, TreeSet<Integer>> h = new HashMap<>();
        List<Integer> ans = new ArrayList<>();

        // Store indices of each number in a TreeSet
        for (int i = 0; i < nums.length; i++) {
            h.computeIfAbsent(nums[i], k -> new TreeSet<>()).add(i);
        }

        // Process each query
        for (int i = 0; i < queries.length; i++) {
            int queryIndex = queries[i];
            int val = nums[queryIndex];
            TreeSet<Integer> indexSet = h.get(val);

            if (indexSet.size() == 1) {
                ans.add(-1);
                continue;
            }

            int minAns = nums.length;

            // Get next occurrence (higher)
            Integer next = indexSet.higher(queryIndex);
            if (next != null) {
                int diff = next - queryIndex;
                minAns = Math.min(minAns, Math.min(diff, nums.length - diff));
            }

            // Get previous occurrence (lower)
            Integer prev = indexSet.lower(queryIndex);
            if (prev != null) {
                int diff = queryIndex - prev;
                minAns = Math.min(minAns, Math.min(diff, nums.length - diff));
            }

            // Handle circular case
            Integer first = indexSet.first();
            Integer last = indexSet.last();
            if (queryIndex == first) {
                int diff = last - first;
                minAns = Math.min(minAns, Math.min(diff, nums.length - diff));
            }
            if (queryIndex == last) {
                int diff = last - first;
                minAns = Math.min(minAns, Math.min(diff, nums.length - diff));
            }

            ans.add(minAns);
        }

        return ans;
    }
}

```
['Java']