Rotating the elements of an array means shifting each element of the array to a new position in a circular manner. The rotation can be either to the left or right, depending on the direction specified.

For example, consider the following integer array:

Original array: {1, 2, 3, 4, 5}

If we rotate this array to the left by 2 positions, it becomes:

Rotated array (left rotation by 2 positions): {3, 4, 5, 1, 2}

Similarly, if we rotate the original array to the right by 3 positions, it becomes:

Rotated array (right rotation by 3 positions): {3, 4, 5, 1, 2}

Essentially, the elements "wrap around" the array, and elements that go beyond the boundary are brought back to the other side.

The process of rotating an array involves shifting the elements and handling the "wrapping around" of elements to the other end. Various techniques can be used to achieve array rotation, such as using extra arrays, using reverse operations, or using cyclic replacements


Here's a Java program to rotate the elements of an array to the left by a given number of positions:


public class ArrayRotation {
public static void main(String[] args) {
// Step 1: Define an integer array 
int[] numbers = { 1, 2, 3, 4, 5 };
// Step 2: Define the number of positions to rotate 
int positionsToRotate = 2;
// Step 3: Call the function to rotate the array 
 rotateLeft(numbers, positionsToRotate);
// Step 4: Print the rotated array 
 System.out.println("Rotated Array: ");
for (int number : numbers) {
 System.out.print(number + " ");
 } 
 } 
// Function to rotate the array to the left
public static void rotateLeft(int[] arr, int positions) {
int n = arr.length; 
// Step 5: Normalize the number of positions to rotate 
 positions = positions % n; 
// Step 6: Reverse the first part of the array 
 reverseArray(arr, 0, positions - 1);
// Step 7: Reverse the second part of the array
reverseArray(arr, positions, n - 1);
// Step 8: Reverse the entire array 
 reverseArray(arr, 0, n - 1); 
 } 
// Function to reverse a subarray within the array 
public static void reverseArray(int[] arr, int start, int end)
while (start < end) {
int temp = arr[start]; 
 arr[start] = arr[end];
 arr[end] = temp; 
 start++;
 end--; }
 } 
}

Explanation of each step:

  1. We start by defining an integer array named numbers. In this example, we have an array with elements {1, 2, 3, 4, 5}.
  2. We define the number of positions to rotate the array to the left. In this case, positionsToRotate is set to 2.
  3. We call the function rotateLeft(numbers, positionsToRotate) to rotate the array numbers to the left by positionsToRotate positions.
  4. We then print the rotated array using a for-each loop to iterate through each element and display it.
  5. The function rotateLeft takes an integer array arr and the number of positions positions to rotate the array to the left.
  6. We normalize the number of positions to rotate to be within the array length to handle cases where positions is greater than the array length.
  7. We use the reverseArray function to perform the array rotation. We break the rotation into three steps:
  8. Reverse the first part of the array up to the positions - 1 index.
  9. Reverse the second part of the array from the positions index up to the last element.
  10. Reverse the entire array to complete the left rotation.
  11. The reverseArray function is used to reverse a subarray within the array. It takes the array arr, start index start, and end index end, and it swaps the elements to reverse the subarray.


When you run this program with the provided array and number of positions to rotate, it will output:

Rotated Array:

3 4 5 1 2

This is the result of rotating the original array {1, 2, 3, 4, 5} to the left by 2 positions. If you modify the array or the number of positions, the program will rotate the array accordingly.