Arrays: Left Rotation

  • + 0 comments

    Java 8

    Time complexity - O(n)

    I calculate each item's new index after d rotations and add it to a new list.

    public static List<Integer> rotLeft(List<Integer> a, int d) {
            // Write your code here
            // d<=n
            List<Integer> rusultList = new ArrayList<>();
            
            for(int i=0; i<a.size(); i++) {
                rusultList.add(a.get((i+d) % a.size()));
            }
            return rusultList;
            
        }