Following are steps. 1) Store last element in a variable say x. 2) Shift all elements one position ahead. 3) Replace first element of array with x.
C++
// C++ code for program
// to cyclically rotate
// an array by one
# include <iostream>
usingnamespacestd;
voidrotate(intarr[], intn)
{
intx = arr[n - 1], i;
for(i = n - 1; i > 0; i--){
arr[i] = arr[i - 1];
}
arr[0] = x;
}
// Driver code
intmain()
{
intarr[] = {1, 2, 3, 4, 5}, i;
intn = sizeof(arr) /
sizeof(arr[0]);
cout << "Given array is \n";
for(i = 0; i < n; i++)
cout << arr[i] << ' ';
rotate(arr, n);
cout << "\nRotated array is\n";
for(i = 0; i < n; i++)
cout << arr[i] << ' ';
return0;
}
// This code is contributed by jit_t
C
#include <stdio.h>
voidrotate(intarr[], intn)
{
intx = arr[n-1], i;
for(i = n-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = x;
}
intmain()
{
intarr[] = {1, 2, 3, 4, 5}, i;
intn = sizeof(arr)/sizeof(arr[0]);
printf("Given array is\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
rotate(arr, n);
printf("\nRotated array is\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return0;
}
Java
importjava.util.Arrays;
publicclassTest
{
staticintarr[] = newint[]{1, 2, 3, 4, 5};
// Method for rotation
staticvoidrotate()
{
intx = arr[arr.length-1], i;
for(i = arr.length-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = x;
}
/* Driver program */
publicstaticvoidmain(String[] args)
{
System.out.println("Given Array is");
System.out.println(Arrays.toString(arr));
rotate();
System.out.println("Rotated Array is");
System.out.println(Arrays.toString(arr));
}
}
Python3
# Python3 code for program to
# cyclically rotate an array by one
# Method for rotation
defrotate(arr, n):
x =arr[n -1]
fori inrange(n -1, 0, -1):
arr[i] =arr[i -1];
arr[0] =x;
# Driver function
arr=[1, 2, 3, 4, 5]
n =len(arr)
print("Given array is")
fori inrange(0, n):
print(arr[i], end =' ')
rotate(arr, n)
print("\nRotated array is")
fori inrange(0, n):
print(arr[i], end =' ')
# This article is contributed
# by saloni1297
C#
// C# code for program to cyclically
// rotate an array by one
usingSystem;
publicclassTest
{
staticint[]arr = newint[]{1, 2, 3, 4, 5};
// Method for rotation
staticvoidrotate()
{
intx = arr[arr.Length - 1], i;
for(i = arr.Length - 1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = x;
}
// Driver Code
publicstaticvoidMain()
{
Console.WriteLine("Given Array is");
stringOriginal_array = string.Join(" ", arr);
Console.WriteLine(Original_array);
rotate();
Console.WriteLine("Rotated Array is");
stringRotated_array = string.Join(" ", arr);
Console.WriteLine(Rotated_array);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP code for program
// to cyclically rotate
// an array by one
functionrotate(&$arr, $n)
{
$x= $arr[$n- 1];
for($i= $n- 1;
$i> 0; $i--)
$arr[$i] = $arr[$i- 1];
$arr[0] = $x;
}
// Driver code
$arr= array(1, 2, 3, 4, 5);
$n= sizeof($arr);
echo"Given array is \n";
for($i= 0; $i< $n; $i++)
echo$arr[$i] . " ";
rotate($arr, $n);
echo"\nRotated array is\n";
for($i= 0; $i< $n; $i++)
echo$arr[$i] . " ";
// This code is contributed
// by ChitraNayal
?>
Javascript
<script>
// JavaScript code for program
// to cyclically rotate
// an array by one
functionrotate(arr, n)
{
varx = arr[n-1], i;
for(i = n-1; i > 0; i--)
arr[i] = arr[i-1];
arr[0] = x;
}
vararr = [1, 2, 3, 4, 5];
varn = arr.length;
document.write("Given array is <br>");
for(vari = 0; i< n; i++)
document.write(arr[i] + " ");
rotate(arr, n);
document.write("<br>Rotated array is <br>");
for(vari = 0; i < n; i++)
document.write(arr[i] + " ");
</script>
Output
Given array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), as we need to iterate through all the elements. Where n is the number of elements in the array. Auxiliary Space: O(1), as we are using constant space.
Another approach:
We can use two pointers, say i and j which point to first and last element of array respectively. As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, so start swapping arr[i] and arr[j] and keep j fixed and i moving towards j. Repeat till i is not equal to j.
C
#include <stdio.h>
voidswap(int*x, int*y)
{
inttemp = *x;
*x = *y;
*y = temp;
}
voidrotate(intarr[], intn)
{
inti = 0, j = n - 1;
while(i != j)
{
swap(&arr[i], &arr[j]);
i++;
}
}
intmain()
{
intarr[] = {1, 2, 3, 4, 5}, i;
intn = sizeof(arr)/sizeof(arr[0]);
printf("Given array is\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
rotate(arr, n);
printf("\nRotated array is\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return0;
}
C++
#include <iostream>
usingnamespacestd;
voidrotate(intarr[], intn)
{
inti = 0, j = n-1; // i and j pointing to first and last element respectively
while(i != j){
swap(arr[i], arr[j]);
i++;
}
}
// Driver code
intmain()
{
intarr[] = {1, 2, 3, 4, 5}, i;
intn = sizeof(arr) /
sizeof(arr[0]);
cout << "Given array is \n";
for(i = 0; i < n; i++)
cout << arr[i] << " ";
rotate(arr, n);
cout << "\nRotated array is\n";
for(i = 0; i < n; i++)
cout << arr[i] << " ";
return0;
}
Java
importjava.util.Arrays;
publicclassTest
{
staticintarr[] = newint[]{1, 2, 3, 4, 5};
staticvoidrotate()
{
inti = 0, j = arr.length - 1;
while(i != j)
{
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
/* Driver program */
publicstaticvoidmain(String[] args)
{
System.out.println("Given Array is");
System.out.println(Arrays.toString(arr));
rotate();
System.out.println("Rotated Array is");
System.out.println(Arrays.toString(arr));
}
}
Python3
defrotate(arr, n):
i =0
j =n -1
whilei !=j:
arr[i], arr[j] =arr[j], arr[i]
i =i +1
pass
# Driver function
arr=[1, 2, 3, 4, 5]
n =len(arr)
print("Given array is")
fori inrange(0, n):
print(arr[i], end =' ')
rotate(arr, n)
print("\nRotated array is")
fori inrange(0, n):
print(arr[i], end =' ')
C#
// C# code for program to cyclically
// rotate an array by one
usingSystem;
classGFG{
staticint[]arr = newint[]{ 1, 2, 3, 4, 5 };
// Method for rotation
staticvoidrotate()
{
intn = arr[arr.Length - 1];
// i and j pointing to first and
// last element respectively
inti = 0, j = n - 1;
while(i != j)
{
{
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
i++;
}
}
// Driver Code
publicstaticvoidMain()
{
Console.WriteLine("Given Array is");
stringOriginal_array = string.Join(" ", arr);
Console.WriteLine(Original_array);
rotate();
Console.WriteLine("Rotated Array is");
stringRotated_array = string.Join(" ", arr);
Console.WriteLine(Rotated_array);
}
}
// This code is contributed by annianni
Javascript
<script>
// JavaScript code for program
// to cyclically rotate
// an array by one using pointers i,j
functionrotate(arr, n){
vari = 0
varj = n-1
while(i != j){
let temp;
temp = arr[i];
arr[i] = arr[j];
arr[j]= temp;
i =i+1
}
}
vararr = [1, 2, 3, 4, 5];
varn = arr.length;
document.write("Given array is <br>");
for(vari = 0; i< n; i++)
document.write(arr[i] + " ");
rotate(arr, n);
document.write("<br>Rotated array is <br>");
for(vari = 0; i < n; i++)
document.write(arr[i] + " ");
</script>
Output
Given array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), as we need to iterate through all the elements. Where n is the number of elements in the array. Auxiliary Space: O(1), as we are using constant space.
Another approach(Using Slicing ):
We can also solve this problem using slicing in python.
Implementation for the above approach:-
C++
#include <iostream>
usingnamespacestd;
voidrotateArray(intarray[], intsize) {
intlast = array[size-1];
for(inti = size-1; i > 0; i--) {
array[i] = array[i-1];
}
array[0] = last;
}
intmain() {
intarray[] = {1, 2, 3, 4, 5};
intsize = sizeof(array) / sizeof(array[0]);
rotateArray(array, size);
for(inti = 0; i < size; i++) {
cout << array[i] << " ";
}
return0;
}
Python3
defrotateArray(array):
'''
array[-1:] taking last element
array[:-1] taking elements from start to last second element
array[:] changing array from starting to end
'''
array[:] =array[-1:]+array[:-1]
# create array
array =[1, 2, 3, 4, 5]
# send array to rotateArray function
rotateArray(array)
print(*array) # 5 1 2 3 4
Java
importjava.util.Arrays;
publicclassMain {
publicstaticvoidrotateArray(int[] array, intsize)
{
intlast = array[size - 1];
for(inti = size - 1; i > 0; i--) {
array[i] = array[i - 1];
}
array[0] = last;
}
publicstaticvoidmain(String[] args)
{
int[] array = { 1, 2, 3, 4, 5};
intsize = array.length;
rotateArray(array, size);
System.out.println(Arrays.toString(array));
}
} // this code is contributed by devendrasalunke
Javascript
functionrotateArray(array) {
const last = array[array.length - 1]; // Store the last element of the array
for(let i = array.length - 1; i > 0; i--) {
array[i] = array[i - 1]; // Shift each element to the right by one
}
array[0] = last; // Replace the first element with the stored last element
}
const array = [1, 2, 3, 4, 5];
rotateArray(array);
console.log(array); // Output: [5, 1, 2, 3, 4]
C#
usingSystem;
classProgram {
// Define a method to rotate an array to the right by one position
staticvoidRotateArray(int[] array, intsize) {
// Store the last element of the array
intlast = array[size - 1];
// Iterate through the array from the second to last element to the first
for(inti = size - 1; i > 0; i--) {
// Move each element one position to the right
array[i] = array[i - 1];
}
// Move the last element to the first position of the array
array[0] = last;
}
staticvoidMain(string[] args) {
// Declare an array of integers
int[] array = { 1, 2, 3, 4, 5 };
// Get the length of the array
intsize = array.Length;
// Rotate the array to the right by one position
RotateArray(array, size);
// Print the elements of the rotated array to the console
for(inti = 0; i < size; i++) {
Console.Write($"{array[i]} ");
}
}
}
Output
5 1 2 3 4
Time Complexity: O(n), as we are reversing the array. Where n is the number of elements in the array. Auxiliary Space: O(1), as we are using constant space.
Another approach(Using Reversal Algorithm ):
The idea is to reverse the array two times. First time we will reverse the first n-1(n=size of array) elements. Finally, we will get our rotated array by reversing the entire array.
C++
// C++ program to cyclically rotate an array by one
#include <iostream>
usingnamespacestd;
intmain()
{
intarr[] = { 1,2,3,4,5};
intn = sizeof(arr) / sizeof(arr[0]);
intk = 1; //No. of rotations
inti, j;
cout << "Given array is \n";
for(i = 0; i < n; i++)
cout << arr[i] << " ";
// Reverse the first n-1 terms
for(i = 0, j = n - k - 1; i < j; i++, j--) {
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for(i = 0, j = n - 1; i < j; i++, j--) {
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
cout << "\nRotated array is\n";
for(i = 0; i < n; i++)
cout << arr[i] << " ";
return0;
}
Java
// Java program to cyclically rotate an array by one
importjava.util.*;
classMain {
publicstaticvoidmain(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5};
intn = arr.length;
intk = 1; // No. of rotations
inti, j;
System.out.println("Given array is ");
for(i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Reverse the first n-1 terms
for(i = 0, j = n - k - 1; i < j; i++, j--) {
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for(i = 0, j = n - 1; i < j; i++, j--) {
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.out.println("\nRotated array is");
for(i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
C#
usingSystem;
classProgram
{
staticvoidMain(string[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
intn = arr.Length;
intk = 1; //No. of rotations
inti, j;
Console.WriteLine("Given array is:");
for(i = 0; i < n; i++)
Console.Write(arr[i] + " ");
// Reverse the first n-1 terms
for(i = 0, j = n - k - 1; i < j; i++, j--)
{
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for(i = 0, j = n - 1; i < j; i++, j--)
{
inttemp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Console.WriteLine("\nRotated array is:");
for(i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.ReadLine();
}
}
// This code is contributed by Prajwal Kandekar
Javascript
let arr = [1, 2, 3, 4, 5];
let n = arr.length;
let k = 1; // No. of rotations
let i, j;
console.log("Given array is");
for(i = 0; i < n; i++) {
process.stdout.write(arr[i] + " ");
}
// Reverse the first n-1 terms
for(i = 0, j = n - k - 1; i < j; i++, j--) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for(i = 0, j = n - 1; i < j; i++, j--) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
console.log("\nRotated array is");
console.log();
for(i = 0; i < n; i++) {
process.stdout.write(arr[i] + " ");
}
Output
Given array is
1 2 3 4 5
Rotated array is
5 1 2 3 4
Time Complexity: O(n), as we are reversing the array. Where n is the number of elements in the array. Auxiliary Space: O(1), as we are using constant space.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Please Login to comment...