• + 0 comments

    Next Java solution consider avoiding extra rotations.

    public static List<Integer> rotateLeft(int d, List<Integer> arr) {
        // Write your code here
            if(arr.size() == d) return arr;
            //Calculate right rotation and 
            //avoid extra rotations 
            //i.e. d = 16, arr.size() = 5 It's necessary just 1 movement to the right
            int rotation = Math.abs(arr.size()-d)%arr.size();
            System.out.println("rotation:"+rotation);
            //execute rotation
            Integer[] rotatedArr = new Integer[arr.size()];
            int index = 0;
            for(Integer val : arr){
                int newIndex = (index+rotation)%(arr.size());
                System.out.println("newIndex: "+newIndex);
                rotatedArr[newIndex] = val;
                index++;
            }
            
            return Arrays.asList(rotatedArr);
        }