Thought process to reach the optimal solution. [C++]
IntuitionI solved this problem during the contest using a very shabby approach [this]. I won’t go into that approach, which is O(NlogN); instead, I will talk ab
# Intuition
I solved this problem during the contest using a very shabby approach [[this](https://leetcode.com/contest/weekly-contest-441/submissions/detail/1575162528/)]. I won’t go into that approach, which is O(NlogN); instead, I will talk about my thought process for the optimized approach (which I figured out well after the contest).

First things first, it would never be fast enough to calculate the minimum distance on the fly—the constraints are too high (1e5). This is a slight hint that some form of precomputation is needed.

My mind then went towards finding the indices where an element appears. I realized I could store each element and its appearances in a map as [value,indexes]. How does this help? With the indices for each value already available, we can easily compute the minimum distance between those indices.

This approach is awesome because we only process each index once, giving it an overall O(N) time complexity. Cool!

# Approach
1. Iterate over nums, and store the index of each value of nums in a map. [map will store key, value pairs in this form -> (value, list of indexes)]
2. Now we **iterate over the map**, and for each entry (which is unique, since we are using a map) we find its indexes. Now we must deal with next challenge: calculating the minimum distance.
3. We have to pay attention to the fact that we are dealing with a **circular array** here. Which means our list of indexes also has to be treated as a circular array. 
4. A key observation is that if the size of the index list is less than 2 
(k < 2), we can simply assign its value as −1, meaning that the element is unique.
5. Since we are treating the arrays as circulars, we will always mod (%) them by their size. **NOTE:** we add k while calculating prevIdx to avoid value of index going to a negative value.
6. After precomputing the minimum distance for every index, we can easily answer each query by accessing the precomputed values.

# Complexity
- Time complexity: Let N be size of nums, M be size of queries.
    1. Creating the map: O(N)
    2. Iterating over the map: O(N) as we see each index only once. 
    3. Creating final answer vector: O(M)

    #####    Thereforce, time complexity: O(N + M) -> O(N)


- Space complexity: Let N be size of nums, M be size of queries.
    1. Creating the map: O(N)
    2. Creating the minDist vector: O(N)
    3. Creating the answer vector: O(M)
    
    #####    Thereforce, space complexity: O(N + M) -> O(N)

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

        for(int i = 0; i < n; i++) mpp[nums[i]].pb(i);

        vector<int> minDist(n, -1);

        for(auto &entry : mpp) {
            vector<int> &idxs = entry.second;
            int k = idxs.size();
            if(k < 2) continue;

            for(int i = 0; i < k; i++) {
                int prevIdx = idxs[(i - 1 + k) % k];
                int d1 = (idxs[i] - prevIdx + n) % n;

                int nxtIdx = idxs[(i + 1) % k];
                int d2 = (nxtIdx - idxs[i] + n) % n;

                minDist[idxs[i]] = min(d1, d2);
            }
        }

        int q = queries.size();
        vector<int> ans(q);
        for(int i = 0; i < q; i++) ans[i] = minDist[queries[i]];

        return ans;
    }
};
```
['Hash Table', 'C++']