Easy to understand and Optimized Solution Using Unordered Map
IntuitionThe problem involves finding the nearest index of an element in a circular array. A naive approach would be to iterate through the array for each query
# Intuition
The problem involves finding the nearest index of an element in a circular array. A naive approach would be to iterate through the array for each query, but this would be inefficient. Instead, we use a hashmap to store the indices of each element in a sorted order, allowing for efficient binary search.

# Approach
# **1. Preprocessing:**

- Use an unordered map to store each unique number in nums as a key and its indices in a sorted vector as values.

- This allows us to efficiently retrieve all occurrences of a number in nums.

# **2. Processing Queries:**

- For each query, retrieve the key (value of nums[curr]) from the map.

- If the key appears only once, return -1 since no other occurrence exists.

- Otherwise, perform a binary search using lower_bound() to locate the index of curr in the stored indices.

# **3. Finding the Closest Index:**

- Using lower_bound(), identify the current index within the vector of indices.

Determine the nearest index in a circular manner:

- The next occurrence is either the next element in the vector or the first element (if at the end of the array(values)).

- The previous occurrence is either the previous element in the vector or the last element (if at the beginning of the array(values)).

- Compute both forward and backward distances and return the minimum.

# Complexity
# **1. Time complexity:**

- Constructing the map: O(n)

- Processing each query:

  Binary search using lower_bound():O(log(k))  (where k is the number of occurrences)

- Constant-time distance calculation:O(1)

**Overall complexity: O(n+qlog(k)) (where q is the number of queries)**

# **2. Space complexity:**

- Storing the map: O(n)

- Output array: O(q)

**Overall: O(n+q)**

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

        // Store indices of each unique element in sorted order
        for (int i = 0; i < n; i++) {
            mp[nums[i]].push_back(i);
        }

        vector<int> ans;
        for (int curr : queries) {
            int key = nums[curr];
            auto& values = mp[key];

            if (values.size() == 1) {
                ans.push_back(-1);
                continue;
            }

            // Using lower_bound to find index in sorted vector
            auto it = lower_bound(values.begin(), values.end(), curr);
            int index = it - values.begin();

            // Finding nearest index in a circular manner
            int next_index = values[(index + 1) % values.size()];
            int prev_index = values[(index - 1 + values.size()) % values.size()];

            // Calculating distances
            int diff1 = (next_index >= curr) ? (next_index - curr) : (n - curr + next_index);
            int diff2 = (prev_index <= curr) ? (curr - prev_index) : (n - prev_index + curr);

            ans.push_back(min(diff1, diff2));
        }
        return ans;
    }
};

```
['Array', 'Hash Table', 'Binary Search', 'C++']