Java || HashMap || Easy || O(N+q)
IntuitionKeep track of last_seen and first_seen for each num
Maintain a map for the sameApproachEvery occurence of num affects the distance of the first_seen an
# Intuition
Keep track of last_seen and first_seen for each num
Maintain a map for the same

# Approach
Every occurence of num affects the distance of the first_seen and last_seen.so keep minimizing the distance during traversal
and update last_seen

# Complexity
- Time complexity:
O(N+Q)

- Space complexity:
O(N+Q)
# Code
```java []
class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        HashMap<Integer, int[]> map = new HashMap<>();
        int n = nums.length;
        int[] ans = new int[n];
        Arrays.fill(ans, Integer.MAX_VALUE);
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (map.containsKey(nums[i])) {
                int[] arr = map.get(nums[i]);
                int last_d = i - arr[1];
                int first_d = n - (i - arr[0]);
                ans[i] = Math.min(first_d, last_d);
                ans[arr[1]] = Math.min(ans[arr[1]], last_d);
                ans[arr[0]] = Math.min(first_d, ans[arr[0]]);
                map.get(nums[i])[1] = i;
            } else map.put(nums[i], new int[]{i, i});
        }
        for (int qu : queries) {
            result.add(ans[qu] == Integer.MAX_VALUE ? -1 : ans[qu]);
        }
        return result;
    }
}
```
['Hash Table', 'Java']