Doubly Linked List Idea
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
```java []
class Node{
    int prevIndex = -1;
    int currentIndex = -1;
    int nextIndex = -1;
}
class Solution {

    public List<Integer> solveQueries(int[] nums, int[] queries) {

        int len = nums.length;
        int lent = queries.length;

        HashMap<Integer,Node> indexNode = new HashMap<>();

        HashMap<Integer,Node> oneStepBackNode = new HashMap<>();

        HashMap<Integer,Node> LeftMostIndexOfVal = new HashMap<>();
        HashMap<Integer,Node> rightMostIndexOfVal = new HashMap<>();

        for(int i=0;i<len;i++){

            int val = nums[i];


            Node currentNode = new Node();
            currentNode.currentIndex = i;

            if(oneStepBackNode.get(val)==null){

                indexNode.put(i,currentNode);
                oneStepBackNode.put(val,currentNode);

                LeftMostIndexOfVal.put(val,currentNode);
                rightMostIndexOfVal.put(val,currentNode);

            }else{

                Node prev = oneStepBackNode.get(val);
                prev.nextIndex = i;

                currentNode.prevIndex = prev.currentIndex;

                indexNode.put(i,currentNode);
                oneStepBackNode.put(val,currentNode);

                rightMostIndexOfVal.put(val,currentNode);
            }
         
        }

        List<Integer> ans =  new ArrayList<>();

        for(int i = 0; i < lent; i++){

            int index = queries[i];
            int indexVal = nums[index];

            Node node = indexNode.get(index);

            int prevIndex = node.prevIndex;
            int currentIndex = node.currentIndex;
            int nextIndex = node.nextIndex;

            int distToLeftIndex = 0;
            int distToRightIndex = 0;

            if(prevIndex!=-1){

                distToLeftIndex = currentIndex - prevIndex;
            }

            if(nextIndex!=-1){

                distToRightIndex = nextIndex - currentIndex;
            }

            if(prevIndex!=-1 && nextIndex!=-1){

                int min = distToLeftIndex;

                if(distToRightIndex<min)
                min = distToRightIndex;

                ans.add(min);

            }
            else if(prevIndex==-1 && nextIndex==-1){
                ans.add(-1);
            }else if(prevIndex==-1){

                Node rightMost = rightMostIndexOfVal.get(indexVal);
                
                int rigthMostCurrentIndex = rightMost.currentIndex;

                int rightDist = len - rigthMostCurrentIndex;

                int leftDist  = currentIndex;

                int totDist = leftDist + rightDist;
                int min = totDist;

                if(distToRightIndex<totDist)
                min = distToRightIndex;

                ans.add(min);

            }else if(nextIndex==-1){

                   Node leftMost = LeftMostIndexOfVal.get(indexVal);

                   int leftDist = leftMost.currentIndex;

                   int rightDist = len - currentIndex;

                   int totDist = leftDist + rightDist;

                  int min = totDist;

                     if(distToLeftIndex<totDist)
                min = distToLeftIndex;

                ans.add(min);

            }



        }


        return ans;
        
    }
}
```
['Java']