Intuitive ✅ In-depth Explanation 💯
Understanding the Function Signature
nums: A list of integers.
queries: A list of indices referring to elements in nums.
The function returns a list of integers

## **Understanding the Function Signature**
```python
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
```
- `nums`: A list of integers.
- `queries`: A list of indices referring to elements in `nums`.
- The function returns a list of integers representing answers to the queries.

---

## **Key Variables and Data Structures**
```python
hashmap = defaultdict(int) 
```
- `hashmap` stores the last seen index of each number in `nums`.

```python
temp = nums + nums
```
- `temp` is a duplicated version of `nums` (`nums` repeated twice).
- This effectively simulates a circular array.

```python
res = [float("inf")] * len(nums)
```
- `res` keeps track of the **smallest distance to the next occurrence** of each element in `nums`.
- Initialized with `inf` because initially, no distances are known.

```python
n = len(nums)
```
- `n` is the original length of `nums`.

---

## **First Loop: Computing Minimum Distances**
```python
for i in range(len(temp)):
```
- The loop iterates over `temp`, which is `2 * len(nums)`, allowing us to handle circular properties.

### **Processing Each Element**
```python
if nums[i % n] in hashmap:
```
- `i % n` ensures that we always refer to an index within `nums`, making `temp` act like a circular array.
- If the number `nums[i % n]` has been seen before, we check its previous occurrence.

#### **Avoid Redundant Comparisons**
```python
if hashmap[nums[i % n]] == i % n:
    continue
```
- If the stored index in `hashmap` is the same as `i % n`, it means we are revisiting the same occurrence, so we skip.

#### **Calculate Distance Between Repeated Elements**
```python
j = hashmap[nums[i % n]]
diff = abs(i - j)
```
- `j` is the last recorded index of `nums[i % n]`.
- `diff` computes the absolute distance between the two occurrences.

#### **Update the Minimum Distances**
```python
res[i % n] = min(diff, res[i % n])
res[j] = min(diff, res[j])
```
- Updates `res` to store the **smallest** distance for both indices.

#### **Update `hashmap` with the Most Recent Index**
```python
hashmap[nums[i % n]] = i % n
```
- Stores the most recent occurrence of `nums[i % n]`.

---

## **Handling Queries**
```python
ans = []
for num in queries:
    ans.append(res[num] if res[num] != float("inf") else -1)
```
- For each `num` (which represents an index in `nums`), we check `res[num]`:
  - If `res[num]` is `inf`, no repetition was found, so return `-1`.
  - Otherwise, return the smallest distance.

---

## **Summary of the Algorithm**
1. **Extend `nums` to `temp = nums + nums`** to handle circularity.
2. **Iterate through `temp` and store last-seen indices of elements in `hashmap`**.
3. **Calculate the smallest circular distance for each index** and store it in `res`.
4. **Answer each query using `res`**, returning `-1` if no valid distance was found.

---

## **Time Complexity Analysis**
- The main loop runs over `2 * len(nums)`, i.e., **O(n)**.
- Query lookups are **O(1)** each.
- Overall, the approach runs in **O(n + q)**, where `q` is the number of queries.

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        hashmap = defaultdict(int)
        temp = nums + nums
        res = [float("inf")] * len(nums)
        n = len(nums)
        
        for i in range(len(temp)):
            if nums[i % n] in hashmap:
                if hashmap[nums[i % n]] == i % n:
                    continue

                
                j = hashmap[nums[i % n]]
                diff = abs(i - j)

                res[i % n] = min(diff, res[i % n])
                res[j] = min(diff, res[j])

            hashmap[nums[i % n]] = i % n

        ans = []

        for num in queries:
            ans.append(res[num] if res[num] != float("inf") else -1)

        return ans

                
                
        
```
['Hash Table', 'Greedy', 'Python3']