✅Easy Trick (for Circular) | Easy Explanation 💎
Okay so my aim is to share the basic trick that we can use to solve these circular array questions
i also struggled alot with them but this makes it easy to tac
Okay so my aim is to share the basic trick that we can use to solve these circular array questions 
i also struggled alot with them but this makes it easy to tackle circular array 

**And i have also solved the question using circular array in consideration**

- take a pen and right down the array three times and index this as an array from **0 to 3*n-1**

Now we just copy the array nums onto our custom 3*N array using the loop 
```
for(int i = 0 ; i < 3*n ; i++){
    circ[i] = nums[i%n];
}
```
i created a map so that i can acess the indices of each element 
as we are adding the indices while traversing the array thus they are already sorted 

*binarySearch is just used to traverse the indices in log(m) time*

You will easily understand once you make the array into custom 3*N form replicating the array three times 

Hope this trick helps you!!!
# Code
```java []
class Solution {
    int n ;
    public List<Integer> solveQueries(int[] nums, int[] query) {
        n = nums.length ; 
        int[] circ = new int[3*n];
        for(int i = 0 ; i < 3*n ; i++){
            circ[i] = nums[i%n];
        }
        Map<Integer , List<Integer>> map = new HashMap<>();
        for(int i = 0 ; i < 3*n ; i++){
            map.putIfAbsent(circ[i] , new ArrayList<>());
            map.get(circ[i]).add(i);
        }
        List<Integer> res = new ArrayList<>();
        for(Integer idx : query){
            // if(map.get(nums[idx]).size() == 1){
            //     //This wont happen as atleast 3 copies will always be there 
            //     res.add(-1);
            // }else{
                int cirIdx = idx + n ; 
                int i = Collections.binarySearch( map.get(nums[idx]) , cirIdx) ;
                int mini = Integer.MAX_VALUE ; 
                mini = Math.min(cirIdx - map.get(nums[idx]).get(i-1) , map.get(nums[idx]).get(i+1) - cirIdx ) ; 
                if(mini == n){
                    res.add(-1);
                }else{
                    res.add(mini);
                }
            // }
        }
        return res ; 
    }
    public List<Integer> solveQueriesII(int[] nums, int[] query) {
        List<Integer> res = new ArrayList<>();
        Map<Integer , List<Integer>> map = new HashMap<>();
        n = nums.length ; 
        for(int i = 0 ; i < n ; i++){
            map.putIfAbsent(nums[i] , new ArrayList<>());
            map.get(nums[i]).add(i);
        }
        int q = query.length ;
        for(int i = 0 ; i < q ; i++){
            int target = query[i];
            int element = nums[target];
            //Now i need to do a BS on the list of index
            if(map.get(element).size() == 1){
                res.add(-1);
            }else{
                res.add(solveII(map.get(element) , target));
            }
        }
        return res ; 
    }
    public int solveII(List<Integer> ls , int i){
        int idxOfi = Collections.binarySearch(ls , i);
        int res = Integer.MAX_VALUE ;
        int prev = 0 ; 
        int diff1 = 0 ; 
        int s = ls.size() ; 
        if(idxOfi > 0){
            //that is its the last index 
            prev = ls.get(idxOfi - 1);
            res = Math.min(res , Math.min(i - prev , n - (i - prev)));
        }else{
            prev = ls.get(s-1);
            if(prev != i){
                res = Math.min(res , n - (prev-i));
            }
        }
        
        int prior = 0 ; 
        int diff2 = 0 ; 
        if(idxOfi + 1 < ls.size()){
            prior = ls.get(idxOfi+1);
            res = Math.min(res , Math.min(prior - i , n -(prior -i)));
        }else{
            prior = ls.get(0);
            if(prior != i){
                res = Math.min(res , n - (i - prior));
            }
        }
        // return Math.min(diff1 , diff2);
        return res ;
    }
}
```
['Java']