Left Rotate Array in Java: Explanation & Practice

Rotate array elements to the left by k positions

Problem summary

Rotate an array to the left by k positions. Elements that fall off the left appear on the right.

Starter code

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int k = 2; // Test case 1
        
        // Rotate left by k positions
        // Print result space-separated
    }
}

Expected output and test cases

  • Rotate [1,2,3,4,5] left by 2
    3 4 5 1 2
  • Rotate by 1
    2 3 4 5 1
  • Rotate by 5 (full rotation)
    1 2 3 4 5

Hints

  1. k = k % length handles k > length
  2. Store first k elements temporarily
  3. Shift remaining elements left, then add stored elements

Related Arrays exercises

Practice all Arrays exercises · Run this idea in the Java compiler