Rotate array to the left I am a beginner in programming. To rotate the array to left, I have written the below code. Please point out how to optimize the code and anything I am doing which is not good programming practice.

package arrays;

import java.util.Scanner;

public class ArrayLeftRotation {

    public static int[] LeftRotation(int numberOfLeftRotations, int[] a, int[] b) {

        int n1 = numberOfLeftRotations, n2 = numberOfLeftRotations;
        /*
         * First for loop Given array a0=1,a1=2,a3=4,a4=5,a5=6,a6=7; First we
         * are shifting the first 3 elements to last 3 slots and the array looks
         * like below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; The above step we are
         * performing through below logic
         * a[array.length-numberOfLeftRotations]=b[i]; GIVEN ARRAY LENGTH -
         * NUMBER OF LEFT ROTATIONS IS THE STARTING POINT TO MOVE THE FIRST
         * ELEMENTS OF THE ARRAY TO LAST
         */
        for (int i = 0; i &lt; numberOfLeftRotations; i++) {
            a[a.length - n1] = b[i];
            n1--;
        }
        /*
         * Second for loop After executing the above loop the array look like
         * below a0=1,a1=2,a2=3,a3=,a4=1,a5=2,a6=3; now except the last three
         * slots every thing needs to be changed
         * a0=4,a1=5,a2=6,a3=7,a4=1,a5=2,a6=3; we achieved the above array
         * result through below logic a[i]=b[numberOfLeftRotations]; REPLACED
         * THE REMAINING SLOTS WITH THE VALUES STARTING FROM
         * NUMBEROFLEFTROTATIONS
         */
        for (int i = 0; i &lt; a.length - numberOfLeftRotations; i++) {
            a[i] = b[n2];
            n2++;
        }
        return a;
    }

    public static int[] LeftRotation(int numberOfLeftRotations, int[] a) {

        return a;

    }

    public static int[] printArray(int[] a) {

        for (int i = 0; i &lt; a.length; i++) {
            System.out.println(a[i]);
        }
        return a;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of Left Rotations need to be done");
        int l = sc.nextInt();
        System.out.println("Enter number of elements in an array");
        int e = sc.nextInt();
        int[] a = new int[e];

        for (int i = 0; i &lt; a.length; i++) {
            a[i] = sc.nextInt();
        }

        int[] b = a.clone();

        LeftRotation(l, a, b);
        printArray(a);

    }
}
Your class has 4 methods:


int[] LeftRotate(int, int[], int[])
int[] LeftRotate(int, int[])
int[] printArray(int[])
void main(String[])


The second method is never called, and can be removed.

The first and third methods (and the second) all return an int[], the a parameter, but the returned values are never assigned to anything.  This return value can be removed, and the methods declared to return void.

In the first method, you are looping over increasing i values, decrementing n1, and assigning to a[a.length - n1].  This “double negative” makes the code harder to understand; decrementing n1 increases the destination index value.  You already have a i index which in incrementing.  You could instead:

for(int i = 0; i &lt; numberOfLeftRotations; i++) {
    a[a.length - numberOfLeftRotations + i] = b[i];
}


This is a little easier to understand; you are only changing i, increasing it, and adding it to a starting point for the destination.  We can make it a little clearer:

int dest_start = a.length - numberOfLeftRotations;
for(int i = 0; i &lt; numberOfLeftRotations; i++) {
    a[dest_start + i] = b[i];
}


That is much more understandable.  Do the same for the second loop.



The above is just a cleanup of your existing code.  We can restructure / refactor the code to make it better and easier to use.

When you want to left-rotate an array, you always need to create a new array for the destination.  You can make the left-rotate function create the destination array for the caller.

int[] LeftRotate(int numberOfLeftRotations, int[] source) {
    int[] dest = new int[source.length];
    // ... copy rotated version from dest into source
    return dest;
}


And use it as:

int[] b = LeftRotation(l, a);


In fact, this is probably what was intended for your second method.  The code to copy the rotated version from dest to source could just be a call of the first “helper” method:

int[] LeftRotate(int numberOfLeftRotations, int[] source) {
    int[] dest = new int[source.length];
    LeftRotate(numberOfLeftRotations, dest, source);
    return dest;
}


Your first method should still be void, as it is not returning anything that wasn’t just passed to it.



Copying sequential elements from one array to another is a common operation, and Java provides a method which does just that: System.arraycopy().  Using this, your left rotation can be performed very efficiently in two statements:

System.arraycopy(source, 0, dest, dest.length - numberOfLeftRotations, numberOfLeftRotations);
System.arraycopy(source, numberOfLeftRotations, dest, 0, dest.length - numberOfLeftRotations);




Use better variable names.  a and b are unclear; source and dest give context to the reader.  numberOfLeftRotations is a very descriptive, but it is perhaps a little long.  numLeftRotations is just as clear and doesn’t result in lines becoming excessively long.  numRotations or rotations might be even better; the reader would know they are left rotations, because the variables are contained within a “left rotate” function.

Convention is for only Class names to begin with upper case letters; method names should not.  You should name the method leftRotate, not LeftRotate.