C++ | Easy | Binary Search | beats 100%
IntuitionApproach
Preprocessing Using a Hash Map, You use an unordered_map<int, vector> mp to store the indices of each unique number in nums. This helps in eff
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
- Preprocessing Using a Hash Map, You use an unordered_map<int, vector<int>> mp to store the indices of each unique number in nums. This helps in efficiently looking up the positions of occurrences of any number.
You iterate through nums and populate mp, where mp[val] stores all indices where val appears.
- For each query queries[i], which represents an index in nums, dentify the number nums[queries[i]] that needs to be processed. Use mp[nums[queries[i]]] to retrieve the list of indices where this number appears.
- Perform a binary search on mp[nums[queries[i]]] to locate the exact position x of queries[i] in the list. Once found, you determine the closest occurrence by considering. The left neighbor (previous occurrence). The right neighbor (next occurrence).
-  Since the array is circular, you handle wrap-around cases using modular arithmetic.
- If x-1 (previous occurrence) exists, compute the absolute difference and consider the circular distance (n - difference). If x+1 (next occurrence) exists, compute the absolute difference similarly. Take the minimum of these distances to find the closest occurrence.
- If the number occurs only once, return -1 as no other occurrence exists

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

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

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        unordered_map<int,vector<int>>mp;
        int n = nums.size();
        for(int i=0;i<nums.size();i++){
            mp[nums[i]].push_back(i);
        }
        vector<int>ans(queries.size());
        for(int i=0;i<queries.size();i++){
                int mn =INT_MAX;
                // vector<int>temp = mp[nums[queries[i]]];
            int l = 0, r = mp[nums[queries[i]]].size()-1,x=0;
            while(l<=r){
                int mid = (l+r)/2;
                if(mp[nums[queries[i]]][mid]==queries[i]){
                    x= mid;
                    break;
                }else if(mp[nums[queries[i]]][mid]<queries[i]){
                    l = mid+1;
                }else{
                    r = mid-1;
                }
            }

            int len = mp[nums[queries[i]]].size();

            if( mp[nums[queries[i]]].size()>1){
                int left = (x-1+len)%len;
                int right = (x+1+len)%len;
                if(x-1>=0){
                    mn = min(abs(queries[i]-mp[nums[queries[i]]][left]),min(mn,n-(queries[i]-mp[nums[queries[i]]][left])));
                }else{
                    mn = min(abs(mp[nums[queries[i]]][left]-queries[i]),min(mn,abs(n-(mp[nums[queries[i]]][left]-queries[i]))));
                }
                if(x+1<len){
                    mn = min(abs(mp[nums[queries[i]]][right]-queries[i]),min(mn,abs(n-(mp[nums[queries[i]]][right]-queries[i]))));
                }else{
                    mn = min(abs(queries[i]-mp[nums[queries[i]]][right]),min(mn,abs(n-(queries[i]-mp[nums[queries[i]]][right]))));
                }
            }
          
            if(mn!=INT_MAX)
            ans[i]=(mn);
            else
           ans[i]=-1;
            
        }
        return ans;
    }
};
```
['Binary Search', 'C++']