Easy Java Solution || Beats 100.00% 👋 ||  🚀🚀 || 🔥🔥
IntuitionApproachComplexity
Time complexity: O((N+M)Logm)

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

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

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

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

# Code
```java []

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> res= new ArrayList<>();

        for (int q : queries) {
            int value = nums[q];  
            List<Integer> pos = map.get(value);
            
            if (pos.size() < 2) {
                res.add(-1);
                continue;
            }

        int p = Collections.binarySearch(pos, q);
        int size = pos.size();

            int left = pos.get((p- 1 + size) % size);
             int right = pos.get((p + 1) % size);

            int minDist = Math.min(distance(q,left,n), 
                                distance(q,right,n));
            res.add(minDist);
        }

        return res;
    }
    private int distance(int a,int b,int n) {
        int diff = Math.abs(a-b);
        return Math.min(diff,n -diff);
    }
}

```
['Java']