Hashing | Binary Search | Greedy | Implementation | Easy Explanation | Beats 100%
IntuitionHere, we are required to find the minimum distance between two same elements given in an array. The twist is, given an array queries which has the requ
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Here, we are required to find the minimum distance between two same elements given in an array. The twist is, given an array `queries` which has the required `indices` to check and report the distance, we need to store the answer with respect to this particular index, ie the minimum of this index to the most closest one. Also the array given is considered to be cyclic.

To approach this question, we first store all the indices for each element where they are present in a Hashmap.
Then we loop across `queries` and for each index, we check whether the element at that index, if present just once in the array `nums` or multiple times. If it present only once simply add `-1` to the answer.
Else find the closet index to this index from the hashmap.

# Approach
<!-- Describe your approach to solving the problem. -->
In order to find the minimum distance between the indices of a particular element, we first find the index `ind` of the current query index `queries[i]` in the vector of that particular number `nums[queries[i]]`.
After fetching the index `ind`, we the two closet distance, one to left and other to the right (for both we assume the array to be circular)

> (A greedy approach, as the array of indices is already sorted so the just left and right indices will be at least distance from the current index `queries[i]`).

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

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
![image.png](https://assets.leetcode.com/users/images/e211223c-0d4d-4e63-8b5d-e8b6aa423a82_1742103065.6303382.png)

# Code
```cpp []
class Solution {
public:
    int find(int i, int j, int n){
        int temp = abs(i-j);
        return min(temp, n - temp);
    }
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        unordered_map<int, vector<int>> mp;
        for(int i = 0; i<n; i++){
            mp[nums[i]].push_back(i);
        }

        vector<int>ans;
        for(int i = 0; i<queries.size(); i++){
            vector<int>&indices = mp[nums[queries[i]]];
            if(indices.size()==1) ans.push_back(-1);
            else{
                int ans_Ind = INT_MAX; 
                int ind = lower_bound(indices.begin(), indices.end(), queries[i]) - indices.begin();
                int m = indices.size();
                if(ind != 0){
                    int prev = indices[ind - 1];
                    ans_Ind = min(ans_Ind, find(queries[i], prev,n));
                }
                else{
                    int last = indices[m-1];
                    ans_Ind= min(ans_Ind, find(queries[i], last, n));
                }

                if(ind + 1 != m){
                    int next = indices[ind + 1];
                    ans_Ind = min(ans_Ind, find(queries[i], next, n));
                }
                else{
                    int next = indices[0];
                    ans_Ind = min(ans_Ind, find(queries[i], next, n));
                }
                ans.push_back(ans_Ind);
            }
        }
        return ans;
    }
};
```
['Array', 'Hash Table', 'Binary Search', 'Greedy', 'C++']