Java Program to Cyclically Permute the Elements of an Array
Last Updated :
17 Nov, 2022
Given an array of integers, there we 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:
Input: [1,2,3,4,5]
Output: [2,3,4,5,1]
Input: [2,3,1,5,6]
Output: [3,1,5,6,2]
Approach #1
- In function cyclicShift(), the loop for(i=0; i<arr.length; i++) traverses through the array and shifts each element one position before.
- The first value of the array is stored in variable x before the loop.
- Finally, the last element of the array is set to x.
Java
// Java Program to Cyclically Permute
// the Elements of an Array
import java.util.*;
import java.io.*;
// function to print the array
class cycle {
public int[] cycleShift(int[] 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;
return arr;
}
}
public class GFG {
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
cycle c = new cycle();
int[] newArray = c.cycleShift(arr);
for (int i = 0; i < newArray.length; i++) {
System.out.print(newArray[i] + " ");
}
}
}
- Time Complexity: O(n), where n is the number of elements in the array.
- Space Complexity: O(1)
Approach #2: Using Swapping
In the function cyclicSwap(arr) the loop for(int i = 0; i < arr.length; i++) the swap the first element to its next element in the array
- In First Iteration after swapping it will be, original array [1, 2, 3, 4, 5] --> [2, 1, 3, 4, 5];
- In the second Iteration again after swapping [2, 1, 3, 4, 5] --> [2, 3, 1, 4, 5];
- And this iteration is going on till the loop end Final result would be like this [2, 3, 4, 5, 1]
Below is the implementation of the above approach.
Java
// Java Program to Cyclically Permute
// the Elements of an Array
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
int first = arr[0];
int start = 0;
// swapping each element with the first
// element
for (int i = 1; i < arr.length; i++) {
arr[start++] = arr[i];
arr[i] = first;
}
// Printing the element in the
// array.......
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
- Time Complexity: O(n)
- Space Complexity: O(1)
Approach #3: using the Queue to make the cyclic permute in the array
First, insert all the elements into the queue of from index 1 to arr.length;
dequeue the queue and store back to the array and at last, put the first element to the last index of the array
Java
// Java Program to Cyclically Permute
// the Elements of an Array
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// use of the queue to do
// cyclic shift in the array
int[] arr = { 1, 2, 3, 4, 5 };
Queue<Integer> q = new LinkedList<>();
int first = arr[0];
int strt = 0;
// adding each element into the queue
for (int i = 1; i < arr.length; i++) {
q.add(arr[i]);
}
while (!q.isEmpty()) {
arr[strt++] = q.poll();
}
// Polling out the element from the
// Queue and inserting into the queue
arr[arr.length - 1] = first;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
- Time Complexity: O(n)
- Space Complexity: O(n)
Similar Reads
Java Program to Left Rotate the Elements of an Array In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by "
5 min read
How to Shuffle the Elements of Array in Java? In Java, to shuffle the elements of an array, we can use the shuffle() method from the Collections class. This method is part of the java.util package. To shuffle an array, first, we need to convert the array into a List, shuffle it, and then convert it back to an array if needed. Example:Let's go t
3 min read
Java Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
3 min read
Java Program to Rotate Matrix Elements Given a matrix, clockwise rotate elements in it. Examples: Input 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12Recommended: Please solve it on âPRACTICE â first, before moving on to the solution.
3 min read
Java Program to Rotate Matrix Elements A matrix is simply a two-dimensional array. So, the goal is to deal with fixed indices at which elements are present and to perform operations on indexes such that elements on the addressed should be swapped in such a manner that it should look out as the matrix is rotated.For a given matrix, the ta
6 min read