Simple Easy Binary Search || readable code ✅✅
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 {
    int n ;

    int lowerBound(List<Integer> li,int x) {
        int n = li.size();
        int low = 0, high = n - 1;
        int ans = n;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (li.get(mid)>= x) {
                ans = mid;
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return ans;
    }

    int find(int q, List<Integer> li) {
        int pos = lowerBound(li,q);
        if (pos < 0) pos = -pos - 1;
        int minDist = Integer.MAX_VALUE;
        if (pos > 0) minDist = Math.min(minDist, calc(q, li.get(pos - 1)));
        if (pos < li.size() - 1) minDist = Math.min(minDist, calc(q, li.get(pos + 1)));
        if (pos == 0) minDist = Math.min(minDist, calc(q, li.get(li.size() - 1)));
        if (pos == li.size() - 1) minDist = Math.min(minDist, calc(q, li.get(0)));
        return minDist;
    }

    int calc(int x, int y) {
        return Math.min(Math.abs(x - y), n - Math.abs(x - y));
    }

    public List<Integer> solveQueries(int[] nums, int[] queries) {
        List<Integer> ans = new ArrayList();
        n = nums.length;
        Map<Integer,List<Integer>> map = new HashMap();
        Map<Integer,Integer> idx = new HashMap();
        for(int i=0;i<n;i++){
            idx.put(i,nums[i]);
            if(map.containsKey(nums[i])){
                map.get(nums[i]).add(i);
            }
            else{
                map.put(nums[i],new ArrayList());
                map.get(nums[i]).add(i);
            }
        }
        for(int x : queries){
            int cur = idx.get(x);
            if(map.get(cur).size()==1) ans.add(-1);
            else{
                List<Integer> li = map.get(cur);
                int dist = (int)1e7;
                int i = lowerBound(li,x);
                dist = find(x,li);
                ans.add(dist);
            }
        }
        return ans;
    }
}
```
['Array', 'Hash Table', 'Binary Search', 'C++', 'Java']