O(n) Track last occur index of elements using hashmap.
IntuitionApproachComplexity
Time complexity:
O(n)
Space complexity:
O(n)Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n) 
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n)
# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        nums.insert(nums.end(),nums.begin(),nums.end()); // lazy way dealing with circular array
        vector<int> distance(2*n,INT_MAX); // distance vector

        unordered_map<int, int> map; // num -> last occur idx
        for (int i = 0; i < nums.size(); i ++){
            int num = nums[i], d = INT_MAX;
            if (map.find(num) != map.end()){
                int idx = map[num] , dist = distance[idx];
                if (i - idx < n) // valid length must be less than array length
                    d = i - idx;
                distance[idx] = min(d,dist); // update last occur num's distance
            }
            map[num] = i; // update last occur idx of num
            distance[i] = d; // update distance at i
        }
        vector<int> res;
        for (int i=0; i < queries.size(); i++){
            int r = min(distance[queries[i]], distance[queries[i]+n]); // pick the smaller distance
            if (r != INT_MAX)
                res.push_back(r);
            else
                res.push_back(-1);
        }
        return res;

    }
};
```
['C++']