This is the Java Program to Rotate an Array by n Elements.
Given an array of integers, circularly rotate the elements of the array, by a given value, say n.
Example:
int array[] = {1,2,3,4,5}
n = 3
output = {3,4,5,1,2}
The idea is to use two loops. The outer loop is used to manage the value by which the array elements are to be rotated. In the second loop, just move each array element one position ahead, that is, 1st into 2nd, 2nd into 3rd, and so on, until the last element is moved into the 1st position.
Here is the source code of the Java Program to Rotate an Array by n Elements. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Rotate an Array by n Elements.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RotateArray {
// Function to rotate the array elements
static void rotateArray(int[] array, int n){
int i,j,temp,temp1;
for(i=1;i<=n;i++){
temp = array[0];
for(j=0;j<array.length;j++){
temp1 = array[(j+1) % array.length];
array[(j+1) % array.length] = temp;
temp = temp1;
}
}
}
// main function to read the array and display the output
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size;
System.out.println("Enter the size of the array");
try {
size = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Invalid Input");
return;
}
int[] array = new int[size];
System.out.println("Enter array elements");
int i;
for (i = 0; i < array.length; i++) {
try {
array[i] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
return;
}
}
System.out.println("The contents of the array before rotation are");
for(i=0;i<array.length;i++){
System.out.print(array[i] + " ");
}
System.out.println();
int n;
System.out.println("Enter the number by which the array elements are to "
+ "be rotated");
try{
n=Integer.parseInt(br.readLine());
}catch (Exception e){
System.out.println("An error occurred");
return;
}
rotateArray(array,n);
System.out.println("The contents of the array after rotation are");
for(i=0;i<array.length;i++){
System.out.print(array[i] + " ");
}
}
}
1. In the function rotateArray(), the loop for(i=1; i<=n; i++) runs the number of times, the array is to be rotated.
2. Using a nested loop for(i=0; i<array.length; i++), we shift each array element one position ahead.
3. Therefore, in one rotation of the outer loop, the array is rotated one time. Hence, in n iterations, it is rotated n times.
Time Complexity: O(n*m) where n is the value by which array elements are to be rotated, m is the number of elements in the array.
Case 1 (Positive test Case): Enter the size of the array 5 Enter array elements 1 2 3 4 5 The contents of the array before rotation are 1 2 3 4 5 Enter the number by which the array elements are to be rotated 3 The contents of the array after rotation are 3 4 5 1 2 Case 2 (Positive test Case - another example): Enter the size of the array 7 Enter array elements 234 35 31 46 2434 133 457 The contents of the array before rotation are 234 35 31 46 2434 133 457 Enter the number by which the array elements are to be rotated 8 The contents of the array after rotation are 457 234 35 31 46 2434 133
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice BCA MCQs
- Check Java Books
- Apply for Computer Science Internship
- Practice Programming MCQs
- Apply for Java Internship