Java Program to Cyclically Permute the Elements of an Array

«
»

This is the Java Program to Cyclically Permute the Elements of an Array.

Problem Description

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.

advertisement

Example:
Array: [1,2,3,4,5]

Output: [2,3,4,5,1]

Problem Solution

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.

Program/Source Code
advertisement
advertisement

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.

  1.  
  2. // Java Program to Cyclically Permute the Elements of an Array.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7.  
  8. public class CyclicallyPermuteArray {
  9.     // Function to cyclically permute the array
  10.     static void cyclicallyPermute(int[] array){
  11.         int x = array[0];
  12.         int i;
  13.         for(i=0; i<array.length-1; i++)
  14.             array[i] = array[i+1];
  15.         array[i] = x;
  16.     }
  17.     // Function to read user input and display the output
  18.     public static void main(String[] args) {
  19.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  20.         int size;
  21.         System.out.println("Enter the size of the array");
  22.         try {
  23.             size = Integer.parseInt(br.readLine());
  24.         } catch (Exception e) {
  25.             System.out.println("Invalid Input");
  26.             return;
  27.         }
  28.         int[] array = new int[size];
  29.         System.out.println("Enter array elements");
  30.         int i;
  31.         for (i = 0; i < array.length; i++) {
  32.             try {
  33.                 array[i] = Integer.parseInt(br.readLine());
  34.             } catch (Exception e) {
  35.                 System.out.println("An error occurred");
  36.                 return;
  37.             }
  38.         }
  39.         System.out.println("The initial array is");
  40.         System.out.println(Arrays.toString(array));
  41.         System.out.println();
  42.         cyclicallyPermute(array);
  43.         System.out.println("The array after permutation is");
  44.         System.out.println(Arrays.toString(array));
  45.     }
  46. }
Program Explanation

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.

advertisement

Time Complexity: O(n) where n is the number of elements in the array.

Runtime Test Cases
 
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.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.