Solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
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)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        unordered_map<int, int> first, last, pre, suf;
        vector<int> ans(n, 1e9);
        for (int i = 0; i < n; i++) {
            if (first[nums[i]] == 0) {
                first[nums[i]] = i + 1;
            }
            last[nums[i]] = i + 1;

            if (pre[nums[i]] != 0) {
                ans[i] = i - pre[nums[i]] + 1;
            }
            pre[nums[i]] = i + 1;
        }
        for (int i = n - 1; i >= 0; i--) {
            if (suf[nums[i]] != 0) {
                ans[i] = min(suf[nums[i]] - i - 1, ans[i]);
            }
            suf[nums[i]] = i + 1;
        }
        for (int ii = 0; ii < n; ii++) {
            int i = nums[ii];
            if (first[i] != 0) {
                int id = first[i];
                ans[id - 1] = min(ans[id - 1], first[i] + 
                                                     (n - last[i]));
                ans[last[i] - 1] =
                    min(ans[last[i] - 1], first[i] + (n - last[i]));
            }
        }
        vector<int> q(queries.size());
        for (int i = 0; i < queries.size(); i++) {
            q[i] = ans[queries[i]];
            if (q[i] == n)
                q[i] = -1;
        }
        return q;
    }
};
```
['C++']