This is the Java Program to Cyclically Permute the Elements of an Array.
Given an array of integers, cyclically permute its elements, that is, shift each array element to the left by one index. The first value will go into the last index.
Example:
Array: [1,2,3,4,5]
Output: [2,3,4,5,1]
Store the first value in a variable and shift all the remaining elements to the left using a loop. Finally, put the first value into the last index.
Here is the source code of the Java Program to Cyclically Permute the Elements of an Array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
// Java Program to Cyclically Permute the Elements of an Array.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class CyclicallyPermuteArray {
// Function to cyclically permute the array
static void cyclicallyPermute(int[] array){
int x = array[0];
int i;
for(i=0; i<array.length-1; i++)
array[i] = array[i+1];
array[i] = x;
}
// Function to read user input 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 initial array is");
System.out.println(Arrays.toString(array));
System.out.println();
cyclicallyPermute(array);
System.out.println("The array after permutation is");
System.out.println(Arrays.toString(array));
}
}
1. In function cyclicallyPermute(), the loop for(i=0; i<array.length-1; i++) traverses through the array and shifts each element one position before.
2. The first value of the array is stored in variable x before the loop.
3. Finally, the last element of the array is set to x.
Time Complexity: O(n) where n is the number of elements in the array.
Case 1 (Simple Test Case): Enter the size of the array 5 Enter array elements 1 2 3 4 5 The initial array is [1, 2, 3, 4, 5] The array after permutation is [2, 3, 4, 5, 1] Case 2 (Simple Test Case - another example): Enter the size of the array 5 Enter array elements 9 8 7 6 5 The initial array is [9, 8, 7, 6, 5] The array after permutation is [8, 7, 6, 5, 9]
Sanfoundry Global Education & Learning Series – Java Programs.
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice BCA MCQs
- Practice Programming MCQs
- Buy Java Books
- Practice Information Technology MCQs
- Apply for Java Internship