LAST UPDATED: MARCH 5, 2021
Java Program To Cyclically Permute the Elements of an Array
In this tutorial, we will learn how to cyclically permute the elements of an array that is, shift each array element to the left by one index. The first value will go into the last index. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: 2 4 1 5 6 7
Output: 4 1 5 6 7 2
Program 1: Cyclically Permute the Elements of an Array
In this approach, we cyclically permute the elements of an array by shifting the elements one position before.
Algorithm
- Start
- Declare an Array.
- Initialize the Array
- Declare a variable that will store the element at the first index before the loop.
- Use a for loop to iterate through each element of the array.
- Now, shift each element to the left.
- Now, the last element in the modified array will be the starting element.
- Stop
The below program demonstrates how to cyclically permute the elements of an array by traversing and shifting the elements. Declare a variable before the for loop to store the first element of the array. Using a for loop shift the rest of the elements to the left by one position. Lastly, assign the stored value to the last element of the array. Display the result.
/*Java program to cyclically permute the elements of an array*/
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize array
{
arr[i]=sc.nextInt();
}
System.out.println("Initial Array "+Arrays.toString(arr));
int x = arr[0]; // store a[0]
int i;
for (i = 0; i < arr.length - 1; i++) {
// for other element shift left
arr[i] = arr[i + 1];
}
// for the last element in the modified array
// it will be starting element
arr[i] = x;
System.out.println("Updated Array" +Arrays.toString(arr)); //Display the array
}
}
Enter the number of elements:
5
Enter the elements of the array
6 7 8 2 3 4
Initial Array [ 6 7 8 2 3 4 ]
Updated Array [ 7 8 2 3 4 6 ]
Program 2: Cyclically Permute the Elements of an Array
In this approach, we cyclically permute the elements of an array by swapping the elements.
Algorithm
- Start
- Declare an Array.
- Initialize the Array
- Declare another variable to store the first element of the array.
- Use a for loop to swap each element of the array.
- In each iteration swap each element with the first element.
- Continue this swapping of elements in each iteration till the end of the loop.
- Display the updated array.
- Stop.
Below is the code for the same.
The below program demonstrates how to cyclically permute the elements of an array by swapping the elements. Firstly, declare and initialize an array. Declare another variable to store the first element of the array. Use a for loop to traverse through each element of the array. Then, swap each element with the first element of the array. In each iteration, only two elements are swapped. Continue the process till the end of the loop to get the final result.
/*Java program to cyclically permute the elements of an array*/
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize array
{
arr[i]=sc.nextInt();
}
//Display the original array
System.out.println("Initial Array "+Arrays.toString(arr));
int first = arr[0]; //Initialize the first element of the array to a variable
int start = 0;
// swaping each element with the first element
for (int i = 1; i < arr.length; i++) {
arr[start++] = arr[i];
arr[i] = first;
}
//Display the updated array
System.out.println("Updated Array" +Arrays.toString(arr));
}
}
Enter the number of elements:
5
Enter the elements of the array
1 4 3 6 8 2 6 7 9
Initial Array [ 1 4 3 6 8 2 6 7 9 ]
Updated Array [ 4 3 6 8 2 6 7 9 1 ]