 Hash Map + Binary Search | O(n + q × log k) | C++ 199ms Beats 100%
IntuitionTo answer each query efficiently, we need to quickly find other occurrences of the same number (as at index q) and calculate the shortest circular dist
![le.png](https://assets.leetcode.com/users/images/f3da743e-7ef0-40ae-8a34-88a6c7eb4da4_1742132050.1447854.png)

# Intuition
To answer each query efficiently, we need to quickly find other occurrences of the same number (as at index q) and calculate the shortest circular distance to another same number.
A simple brute-force approach would be too slow, so we optimize using a hash map and binary search.

# Approach
- First, we store the indices (positions) of each number in a hash map (unordered_map<int, vector<int>>).
- For each query q, we:
1. Get the number at index q → nums[q].
1. Retrieve all the positions of this number from the map.
1. Use Binary Search (lower_bound()) to find the first position ≥ q in the sorted positions vector.
1. Check both previous and next positions (using iterator arithmetic) to compute the minimum circular distance.
1. Handle edge cases using wrap-around logic (e.g., if q is at the start or end of array, check positions.front()/positions.back()).


# Complexity
- **Time complexity: O(n + q × log k)**  
  - O(n) to build the hash map.  
  - O(log k) for each query using binary search, where k is the number of occurrences of the queried element.  
  - Total queries = q → O(q × log k)

- **Space complexity: O(n)**  
  - For storing positions of elements in the hash map.


# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        unordered_map<int, vector<int>> numPositions;

        // Store the positions of each number
        for (int i = 0; i < n; ++i) {
            numPositions[nums[i]].push_back(i);
        }

        vector<int> answer;

        for (int q : queries) {
            int val = nums[q];
            const vector<int>& positions = numPositions[val];

            if (positions.size() == 1) {
                answer.push_back(-1);
                continue;
            }

            int minDist = INT_MAX;
            // Binary search to find closest positions around index `q`
            auto it = lower_bound(positions.begin(), positions.end(), q);

            // Check previous position
            if (it != positions.begin()) {
                int prev = *(it - 1);
                int dist = min(abs(q - prev), n - abs(q - prev));
                minDist = min(minDist, dist);
            } else {
                // Wrap around from last
                int prev = positions.back();
                int dist = min(abs(q - prev), n - abs(q - prev));
                minDist = min(minDist, dist);
            }
            //check next position
            if ((it + 1) != positions.end()) {
            int next = *(it + 1);
            int dist = min(abs(q - next), n - abs(q - next));
            minDist = min(minDist, dist);
            } else {
            // Wrap around from first
            int next = positions.front();
            int dist = min(abs(q - next), n - abs(q - next));
            minDist = min(minDist, dist);
            }
            answer.push_back(minDist);
        }
        return answer;
    }
};
```
['C++']