Java program to shift array elements to the left



What Is Left Shifting in an Array?

Shifting the array elements to the left by the given number of positions is also known as left rotation of the array. The element at the beginning gets pushed out of the front during the shift, but instead of being deleted, they are re-attached at the end of the array.

Let us consider an example for a better understanding:

Original Array:


After 1 Left Shift:


We can see in the above figure that, after the first shift, the element "1" is pushed out and then reattached at the end of the array.

After 2 Left Shifts:


In the second shift, the element "2" is pushed out and then reattached at the end of the array, and the rest of the elements are shifted to the left side.

Left Shifting in an Array in Java?

We can perform the left shift operation on an array in the following ways:

  • Using the arraycopy() Method
  • Using the Reversal Algorithm

Left Shift Using arraycopy() Method

The arraycopy method of the Array class copies a portion of one array into another array. We will be using the arraycopy() method to perform a left rotation by copying the first few elements to a temp array, shifting the rest to the left side, and putting the saved elements at the end.

Example

The following is an example to shift array elements to the left:

import java.util.Arrays;
public class ShiftArrayLeft {
   public static void main(String[] args) {
      int[] array = {1, 2, 3, 4, 5};
      int shiftCount = 1;
      System.out.println("Original Array: " + Arrays.toString(array));
      leftShift(array, shiftCount);
      System.out.println("Array after left shift: " + Arrays.toString(array));
   }
   public static void leftShift(int[] array, int shiftCount) {
      int n = array.length;
      shiftCount %= n;
      int[] temp = new int[shiftCount];
      System.arraycopy(array, 0, temp, 0, shiftCount);
      System.arraycopy(array, shiftCount, array, 0, n - shiftCount);
      System.arraycopy(temp, 0, array, n - shiftCount, shiftCount);
   }
}

Following is the output of the above program:

Original Array: [1, 2, 3, 4, 5]
Array after left shift: [2, 3, 4, 5, 1]

Left Shift Using The Reversal Algorithm

The reversal algorithm rotates the array to the left by breaking it into two parts: the first k elements (how many positions to rotate the array), and the remaining elements. First, both parts are reversed separately, and then the whole array is reversed.

Example

Below is an example to shift the array elements to the left:

public class Demo {
  public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5, 6};
    int k = 2;
    leftShift(arr, k);
    for (int i : arr) System.out.print(i + " ");
  }
  static void leftShift(int[] arr, int k) {
    int n = arr.length;
    k %= n;
    reverse(arr, 0, k - 1);
    reverse(arr, k, n - 1);
    reverse(arr, 0, n - 1);
  }
  static void reverse(int[] arr, int l, int r) {
    while (l < r) {
      int tmp = arr[l];
      arr[l++] = arr[r];
      arr[r--] = tmp;
    }
  }
}

Following is the output of the above program:

3 4 5 6 1 2
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-08-07T18:19:53+05:30

894 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements