100% Beats || Java Solution
Code


# Code
```java []
class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        HashMap<Integer, List<Integer>> map = new HashMap<>();

        for(int i=0;i<nums.length;i++){
            if(!map.containsKey(nums[i])){
                map.put(nums[i], new ArrayList<>());
            }

            map.get(nums[i]).add(i);
        }

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

        for(int i=0;i<queries.length;i++){
            int givenIndex = queries[i];
            int nearest = Integer.MAX_VALUE;

            List<Integer> list = map.get(nums[givenIndex]);

            // System.out.println(i+" - ");
            // System.out.println(list);

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

            int j=0;
            int k=list.size()-1;

            while(j<k){
                int mid = (j+k)/2;

                   
                 // System.out.println(j+"  "+k+"  "+mid+"  "+nearest);
                if(givenIndex==list.get(mid)){
                    j = mid;
                    break;
                } else if(givenIndex>list.get(mid)){
                    j = mid + 1;
                } else {
                    k = mid - 1;
                }
            }

                            //  System.out.println("got index= "+j+"  "+list.get(j));
// System.out.println(nearest);
            if(j-1>=0){
                nearest = Math.min(nearest, Math.abs(givenIndex-list.get(j-1)));  
            }  else {
                nearest = Math.min(nearest, givenIndex+nums.length-list.get(list.size()-1)); //left circle way
            } 
            // System.out.println(nearest);

            if(j+1<list.size()){
                nearest = Math.min(nearest, Math.abs(givenIndex-list.get(j+1))); 
            } else {
                nearest = Math.min(nearest, nums.length-givenIndex+list.get(0)); //right circle way
            }

            // System.out.println(nearest);
            ans.add(nearest);

        }

        return ans;
    }
}
```
['Array', 'Hash Table', 'Binary Search', 'Java']