using hash map and binary search
IntuitionIn the description, it requires us to find the minimal distance of the same values, so we can build a hash map with keys are nums[i] and a correspondin
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
In the description, it requires us to find the minimal distance of the same values, so we can build a hash map with keys are nums[i] and a corresponding array to store all related indices.

And because of looping through nums array from beginning to its end, it naturally sorts all indices for the same value, then we can apply binary search to find position of queries[j], and using this position to calculate minimal distance of its previous index and next index.

And when we choose its previous index and next index as our target, and we can move from left direction and right direction to each target, due to the nums array is a circular array, we can use %(mod operation) to calculate correct target position, then we can calculate the distances of them and choose the minimum!

# Approach
<!-- Describe your approach to solving the problem. -->
Using hash map with key: nums[i], values: array to store related values.

Binary search to find position of queries[j], and using mod operation to calculate min distance!

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Let n be the length of nums, m be the length of queries, 
and k be the max length of indices array in the map.

For creating map by looping through nums array, it costs O(n).
For looping through queries array and using binary search to find the position of required index, and then calculating min distance, it takes O(mLog(k)).
In sum, it costs O(n + mLog(k)) totally.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
because of distributing nums array values and indices into map, 
it takes O(n) space. 

# Code
```c++ []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int qn = queries.size();
        int n = nums.size();
        
        std::unordered_map<int, std::vector<int>> m;    //val, index array
        for (int i = 0; i < n; ++i) {
            m[nums[i]].push_back(i);
        }
        
        std::vector<int> ans(qn, -1);
        for (int i = 0; i < qn; ++i) {
            int index = queries[i];
            int val = nums[index];
            
            if (m[val].size() == 1) {
                continue;
            }
            
            //find position
            int pos = std::lower_bound(m[val].begin(), m[val].end(), index) - m[val].begin();
                        
            int len = m[val].size();
            int right = (pos + 1) % len;
            int left = (pos - 1 + len) % len;
            int d_right = std::abs(index - m[val][right]);
            int d_left = std::abs(index - m[val][left]);
            d_right = std::min(d_right, n - d_right);
            d_left = std::min(d_left, n - d_left);
            ans[i] = std::min(d_left, d_right);
        }
        
        return ans;
    }
};
```
```java []
class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int qn = queries.length;
        int n = nums.length;

        Map<Integer, List<Integer>> m = new HashMap<>();  // value -> list of indices
        for (int i = 0; i < n; i++) {
            m.putIfAbsent(nums[i], new ArrayList<>());
            m.get(nums[i]).add(i);
        }

        List<Integer> ans = new ArrayList<>(Collections.nCopies(qn, -1));
        for (int i = 0; i < qn; i++) {
            int index = queries[i];
            int val = nums[index];

            if (m.get(val).size() == 1) {
                continue;
            }

            // Find position in the sorted index list
            List<Integer> positions = m.get(val);
            int pos = Collections.binarySearch(positions, index);
            
            int len = positions.size();
            int right = (pos + 1) % len;
            int left = (pos - 1 + len) % len;
            int dRight = Math.abs(index - positions.get(right));
            int dLeft = Math.abs(index - positions.get(left));
            dRight = Math.min(dRight, n - dRight);
            dLeft = Math.min(dLeft, n - dLeft);
            ans.set(i, Math.min(dLeft, dRight));
        }

        return ans;
    }
}
```
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        n = len(nums)
        qn = len(queries)

        m = {}  # value -> list of indices
        for i in range(n):
            m.setdefault(nums[i], []).append(i)

        ans = [-1] * qn
        for i in range(qn):
            index = queries[i]
            val = nums[index]

            if len(m[val]) == 1:
                continue

            # Find position in the sorted index list
            positions = m[val]
            pos = bisect.bisect_left(positions, index)
            
            len_pos = len(positions)
            right = (pos + 1) % len_pos
            left = (pos - 1 + len_pos) % len_pos
            d_right = abs(index - positions[right])
            d_left = abs(index - positions[left])
            d_right = min(d_right, n - d_right)
            d_left = min(d_left, n - d_left)
            ans[i] = min(d_left, d_right)

        return ans
```
['Hash Table', 'Binary Search', 'C++', 'Java', 'Python3']