Simple solution using hashmap and binary Search
ApproachWe can store the indexes of each element in a sorted way, and binary search on itComplexity
Time complexity:

Space complexity:
Code

# Approach
We can store the indexes of each element in a sorted way, and binary search on it

# 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 int binSearch(List<Integer> arr,int idx){
        int l = 0,r=arr.size()-1;
        while (l<=r){
            int mid = l+(r-l)/2;
            if (arr.get(mid) == idx) return mid;
            else if (arr.get(mid)>idx) r=mid-1;
            else l=mid+1;
        }
        return -1;
    }
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        Map<Integer,ArrayList<Integer>> hashmap = new HashMap<>();
        for(int i = 0;i<nums.length;i++){
            if (hashmap.containsKey(nums[i])) hashmap.get(nums[i]).add(i);
            else{
                hashmap.put(nums[i],new ArrayList<>(List.of(i)));
            }
        }
        int n = nums.length;
        List<Integer> ans = new ArrayList<>();
        for(int i = 0;i<queries.length;i++){
            int idx = queries[i];
            int curEle = nums[queries[i]];
            List<Integer> idxList = hashmap.get(curEle);
            if (idxList.size()==1){
                ans.add(-1);
                continue;
            }
            int firstOcc = idxList.get(0);
            int lastOcc = idxList.get(idxList.size()-1);
            if (idx == firstOcc){
                ans.add(Math.min(idxList.get(1)-idx,n-lastOcc+idx));
            }else if (idx == lastOcc){
                ans.add(Math.min(idx-idxList.get(idxList.size()-2),n-idx+firstOcc));
            }else{
                int listIdx = binSearch(idxList,idx);
                ans.add(Math.min(idx - idxList.get(listIdx-1),idxList.get(listIdx+1)-idx));
            }
        }
        return ans;
    }
}
```
['Hash Table', 'Binary Search', 'Java']