Rotate Array By One
Given an array, the task is to cyclically right-rotate the array by one.
Examples:
Input: arr[] = [1, 2, 3, 4, 5]
Output: [5, 1, 2, 3, 4]Input: arr[] = [2, 3, 4, 5, 1]
Output: [1, 2, 3, 4, 5]
Table of Content
Shifting Each Element - O(n) Time and O(1) Space
The basic idea is to store the last element in a temp variable and shift every other element one position ahead. After shifting, update the first element with value stored in temp.
#include <iostream>
#include<vector>
using namespace std;
void rotate(vector<int> &arr) {
int n = arr.size();
// store the last element in a variable
int lastElement = arr[n-1];
// assign every value by its predecessor
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
// first element will be assigned by last element
arr[0] = lastElement;
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5 };
rotate(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << ' ';
return 0;
}
using namespace std;
void rotate(vector<int> &arr) {
int n = arr.size();
// store the last element in a variable
int lastElement = arr[n-1];
// assign every value by its predecessor
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
// first element will be assigned by last element
arr[0] = lastElement;
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5 };
rotate(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << ' ';
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void rotate(int arr[], int n) {
// store the last element in a variable
int lastElement = arr[n-1];
// assign every value by its predecessor
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
// first element will be assigned by last element
arr[0] = lastElement;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
rotate(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
import java.util.Arrays;
class GfG {
static void rotate(int[] arr) {
// store the last element in a variable
int lastElement = arr[arr.length - 1];
// assign every value by its predecessor
for (int i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
// first element will be assigned by last element
arr[0] = lastElement;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
rotate(arr);
System.out.println(Arrays.toString(arr));
}
}
def rotate(arr):
# store the last element in a variable
lastElement = arr[-1]
# assign every value by its predecessor
for i in range(len(arr) - 1, 0, -1):
arr[i] = arr[i - 1]
# first element will be assigned by last element
arr[0] = lastElement
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
rotate(arr)
for i in range(0, len(arr)):
print(arr[i], end=' ')
using System;
class GfG {
static void Rotate(int[] arr) {
// store the last element in a variable
int lastElement = arr[arr.Length - 1];
// assign every value by its predecessor
for (int i = arr.Length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
// first element will be assigned by last element
arr[0] = lastElement;
}
static void Main() {
int[] arr = {1, 2, 3, 4, 5};
Rotate(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
function rotate(arr) {
// store the last element in a variable
const lastElement = arr[arr.length - 1];
// assign every value by its predecessor
for (let i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
// first element will be assigned by last element
arr[0] = lastElement;
}
// Driver Code
const arr = [1, 2, 3, 4, 5];
rotate(arr);
console.log(arr);
Output
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.
Using Two Pointers - O(n) Time and O(1) Space
We can use two pointers, As we know in cyclic rotation we will bring last element to first and shift rest in forward direction, we can do this by swapping every element with last element till we get to the last point.
#include <iostream>
#include<vector>
using namespace std;
void rotate(vector<int> &arr) {
int n=arr.size();
// i and j pointing to first and last
// element respectively
int i = 0, j = n - 1;
while (i != j) {
swap(arr[i], arr[j]);
i++;
}
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5 };
int i;
rotate(arr);
for (i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
using namespace std;
void rotate(vector<int> &arr) {
int n=arr.size();
// i and j pointing to first and last
// element respectively
int i = 0, j = n - 1;
while (i != j) {
swap(arr[i], arr[j]);
i++;
}
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5 };
int i;
rotate(arr);
for (i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void rotate(int *arr, int n) {
// i and j pointing to first and last
// element respectively
int i = 0, j = n - 1;
while (i != j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
rotate(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
import java.util.Arrays;
class GfG {
static void rotate(int[] arr) {
// i and j pointing to first and last
// element respectively
int i = 0, j = arr.length - 1;
while (i != j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
rotate(arr);
System.out.println(Arrays.toString(arr));
}
}
def rotate(arr):
# i and j pointing to first and last
# element respectively
i, j = 0, len(arr) - 1
while i != j:
arr[i], arr[j] = arr[j], arr[i]
i += 1
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
rotate(arr)
for i in range(0, len(arr)):
print(arr[i], end=' ')
using System;
class GfG {
static void Rotate(int[] arr) {
// i and j pointing to first and last
// element respectively
int i = 0, j = arr.Length - 1;
while (i != j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
static void Main() {
int[] arr = {1, 2, 3, 4, 5};
Rotate(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
function rotate(arr) {
// i and j pointing to first and last
// element respectively
let i = 0, j = arr.length - 1;
while (i !== j) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i++;
}
}
// Driver Code
let arr = [1, 2, 3, 4, 5];
rotate(arr);
console.log(arr);
Output
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.
Reversing the Array - O(n) Time and O(1) Space
We can use Reversal Algorithm also, reverse first n-1 elements and then whole array which will result into one right rotation.
#include <iostream>
#include<vector>
using namespace std;
void rotate(vector<int> &arr) {
int n = arr.size();
// Reverse the first n-1 terms
int i, j;
for (i = 0, j = n - 2; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for (i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5 };
int i, j;
rotate(arr);
for (i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
using namespace std;
void rotate(vector<int> &arr) {
int n = arr.size();
// Reverse the first n-1 terms
int i, j;
for (i = 0, j = n - 2; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for (i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5 };
int i, j;
rotate(arr);
for (i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void rotate(int *arr, int n) {
// Reverse the first n-1 terms
int i, j;
for (i = 0, j = n - 2; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for (i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
rotate(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
import java.util.Arrays;
class GfG {
static void rotate(int[] arr) {
int n = arr.length;
// Reverse the first n-1 terms
for (int i = 0, j = n - 2; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for (int i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
rotate(arr);
System.out.println(Arrays.toString(arr));
}
}
def rotate(arr):
n = len(arr)
# Reverse the first n-1 terms
i, j = 0, n - 2
while i < j:
arr[i], arr[j] = arr[j], arr[i]
i += 1
j -= 1
# Reverse the entire array
i, j = 0, n - 1
while i < j:
arr[i], arr[j] = arr[j], arr[i]
i += 1
j -= 1
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
rotate(arr)
for i in range(0, len(arr)):
print(arr[i], end=' ')
using System;
class GfG {
static void Rotate(int[] arr) {
int n = arr.Length;
// Reverse the first n-1 terms
for (int i = 0, j = n - 2; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Reverse the entire array
for (int i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
static void Main() {
int[] arr = {1, 2, 3, 4, 5};
Rotate(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
function rotate(arr) {
const n = arr.length;
// Reverse the first n-1 terms
for (let i = 0, j = n - 2; i < j; i++, j--) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
// Reverse the entire array
for (let i = 0, j = n - 1; i < j; i++, j--) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
// Driver Code
const arr = [1, 2, 3, 4, 5];
rotate(arr);
console.log(arr);
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.