Clean Solution | begginer friendily| O(N)
IntuitionIn the question, they mention finding the minimum distance. Since the array is circular, we can achieve the minimum distance either by using the previo
# Intuition
In the question, they mention finding the minimum distance. Since the array is circular, we can achieve the minimum distance either by using the previous index where the current value appears or the next index where it appears.

# Approach
For finding the previous and next indices, we use maps that store the previous and next indices for the current value.

# Complexity
- Time complexity:
**O(N)**

- Space complexity:
**O(N)**

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        //precomputer first and last index for each index
        
        int n = nums.size();
        
        map<int,int>prevIdx;// [val, idx]
        map<int, int>nextIdx;//[val, idx]
        
        vector<int> prevIdxVal(n,-1);
        vector<int> nextIdxVal(n, -1);
        
        for(int i = 0; i < n; i++){
            
            if(prevIdx.find(nums[i]) != prevIdx.end()){
                prevIdxVal[i] = prevIdx[nums[i]];    
            }
            prevIdx[nums[i]] = i;
            
        }
        
        for(int i = n - 1; i >= 0; i--){
            
            if(nextIdx.find(nums[i]) != nextIdx.end()){
                nextIdxVal[i] = nextIdx[nums[i]];
            }
            nextIdx[nums[i]] = i;
        }
        
        for(int i = 0; i < n; i++){
            
            if(prevIdxVal[i] == -1){
                prevIdxVal[i] = prevIdx[nums[i]];
            }
        }
        
        for(int i = 0; i < n; i++){
            
            if(nextIdxVal[i] == -1){
                nextIdxVal[i] = nextIdx[nums[i]];
            }
        }
        vector<int> ans;
        
        for(auto q : queries){
            int llen = (prevIdxVal[q] > q)?(n - prevIdxVal[q] + q) : (q - prevIdxVal[q]);
            int rlen = (nextIdxVal[q] < q)?(n - q + nextIdxVal[q]) : (nextIdxVal[q] - q);
            
            int val = min(llen, rlen);
            
            ans.push_back(val == 0 ? -1 : val);
        }
        return ans;
    }
};
```
['C++']