Java Program to Rotate an Array by n Elements

This is the Java Program to Rotate an Array by n Elements.

Problem Description

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}

Problem Solution


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.

Program/Source Code

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.

  1.  
  2. //Java Program to Rotate an Array by n Elements.
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class RotateArray {
  8.     // Function to rotate the array elements
  9.     static void rotateArray(int[] array, int n){
  10.         int i,j,temp,temp1;
  11.         for(i=1;i<=n;i++){
  12.             temp = array[0];
  13.             for(j=0;j<array.length;j++){
  14.                 temp1 = array[(j+1) % array.length];
  15.                 array[(j+1) % array.length] = temp;
  16.                 temp = temp1;
  17.             }
  18.         }
  19.     }
  20.     // main function to read the array and display the output
  21.     public static void main(String[] args) {
  22.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23.         int size;
  24.         System.out.println("Enter the size of the array");
  25.         try {
  26.             size = Integer.parseInt(br.readLine());
  27.         } catch (Exception e) {
  28.             System.out.println("Invalid Input");
  29.             return;
  30.         }
  31.         int[] array = new int[size];
  32.         System.out.println("Enter array elements");
  33.         int i;
  34.         for (i = 0; i < array.length; i++) {
  35.             try {
  36.                 array[i] = Integer.parseInt(br.readLine());
  37.             } catch (Exception e) {
  38.                 System.out.println("An error occurred");
  39.                 return;
  40.             }
  41.         }
  42.         System.out.println("The contents of the array before rotation are");
  43.         for(i=0;i<array.length;i++){
  44.             System.out.print(array[i] + " ");
  45.         }
  46.         System.out.println();
  47.         int n;
  48.         System.out.println("Enter the number by which the array elements are to " 
  49.                             + "be rotated");
  50.         try{
  51.             n=Integer.parseInt(br.readLine());
  52.         }catch (Exception e){
  53.             System.out.println("An error occurred");
  54.             return;
  55.         }
  56.         rotateArray(array,n);
  57.         System.out.println("The contents of the array after rotation are");
  58.         for(i=0;i<array.length;i++){
  59.             System.out.print(array[i] + " ");
  60.         }
  61.     }
  62. }
Program Explanation

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.

advertisement

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.

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

🎓 Free Certifications on 300 subjects are live for June 2026. Register Now!

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels
advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations