Introduction
Rotating an array involves shifting the elements of the array either to the left or the right by a specified number of positions. This task is common in various algorithms and applications, such as circular buffers or scheduling tasks. This guide will walk you through writing a Java program that rotates an array to the right by a specified number of positions.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its elements.
- Prompts the user to enter the number of positions by which the array should be rotated.
- Rotates the array to the right by the specified number of positions.
- Displays the rotated array.
Example:
- Input:
- Array:
[1, 2, 3, 4, 5]
- Rotate by:
2
- Array:
- Output:
"Rotated array: [4, 5, 1, 2, 3]"
Solution Steps
- Read the Array Size, Elements, and Rotation Count: Use the
Scanner
class to take the size, elements, and the number of positions to rotate as input from the user. - Rotate the Array: Implement the logic to rotate the array to the right by the specified number of positions.
- Display the Rotated Array: Print the rotated array.
Java Program
// Java Program to Rotate an Array // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class RotateArray { public static void main(String[] args) { // Step 1: Read the size, elements of the array, and rotation count from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] array = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextInt(); } System.out.print("Enter the number of positions to rotate the array: "); int k = scanner.nextInt(); // Step 2: Rotate the array rotateArray(array, k); // Step 3: Display the rotated array System.out.println("Rotated array:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } // Method to rotate the array to the right by k positions public static void rotateArray(int[] array, int k) { int n = array.length; k = k % n; // In case k is greater than the length of the array // Step 2.1: Reverse the whole array reverseArray(array, 0, n - 1); // Step 2.2: Reverse the first k elements reverseArray(array, 0, k - 1); // Step 2.3: Reverse the remaining elements reverseArray(array, k, n - 1); } // Method to reverse a portion of the array public static void reverseArray(int[] array, int start, int end) { while (start < end) { int temp = array[start]; array[start] = array[end]; array[end] = temp; start++; end--; } } }
Explanation
Step 1: Read the Array Size, Elements, and Rotation Count
- The
Scanner
class is used to read the size of the array, the elements of the array, and the number of positions to rotate the array. ThenextInt()
method captures the size, elements, and rotation count.
Step 2: Rotate the Array
- The
rotateArray()
method rotates the array byk
positions to the right:- Calculate Effective Rotations: The number of positions
k
is adjusted usingk = k % n
to handle cases wherek
is greater than the length of the array. - Reverse the Whole Array: The entire array is reversed using the
reverseArray()
method. - Reverse the First
k
Elements: The firstk
elements of the reversed array are reversed again to restore their original order. - Reverse the Remaining Elements: The remaining elements after the first
k
are reversed to restore their order.
- Calculate Effective Rotations: The number of positions
Step 3: Display the Rotated Array
- The program prints the rotated array using a
for
loop.
Helper Method: Reverse the Array
- The
reverseArray()
method reverses a specified portion of the array by swapping elements from the start and end until they meet in the middle.
Output Example
Example 1:
Enter the size of the array: 5 Enter the elements of the array: 1 2 3 4 5 Enter the number of positions to rotate the array: 2 Rotated array: 4 5 1 2 3
Example 2:
Enter the size of the array: 6 Enter the elements of the array: 10 20 30 40 50 60 Enter the number of positions to rotate the array: 3 Rotated array: 40 50 60 10 20 30
Example 3:
Enter the size of the array: 4 Enter the elements of the array: 1 2 3 4 Enter the number of positions to rotate the array: 1 Rotated array: 4 1 2 3
Conclusion
This Java program demonstrates how to rotate an array to the right by a specified number of positions. The program covers essential concepts such as array manipulation, reversing portions of an array, and handling rotations efficiently. This exercise is valuable for understanding in-place array operations and manipulating data structures in Java programming.