weekly contest ques 2 || hashMap+ binary search || java solution
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 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 void getdis(int ind,List<Integer>temp,int n,List<Integer>ans)
    {
        int firstOcc = temp.get(0);
            int lastOcc = temp.get(temp.size()-1);
            if (ind == firstOcc){
                ans.add(Math.min(temp.get(1)-ind,n-lastOcc+ind));
            }else if (ind == lastOcc){
                ans.add(Math.min(ind-temp.get(temp.size()-2),n-ind+firstOcc));
            }else{
                int listIdx = binSearch(temp,ind);
                ans.add(Math.min(ind - temp.get(listIdx-1),temp.get(listIdx+1)-ind));
            }
    }
    public List<Integer> solveQueries(int[] nums, int[] queries) 
    {
        List<Integer>res=new ArrayList<>();
        
        HashMap<Integer,List<Integer>>map=new HashMap<>();

        for(int i=0;i<nums.length;i++)
        {
            if(map.containsKey(nums[i]))
            {
                map.get(nums[i]).add(i);
            }
            else
            {
                List<Integer>temp=new ArrayList<>();
                temp.add(i);
                map.put(nums[i],temp);
            }
        }

        for(int q:queries)
        {
            int val=nums[q];

            List<Integer>temp=map.get(val);
            if(temp.size()==1)
            {
                res.add(-1);
            }
            else
            {
                getdis(q,temp,nums.length,res);
              
            }
        }
        return res;
        
    }
}
```
['Array', 'Hash Table', 'Binary Search', 'Java']