Given an array, rotate the array by one position in clock-wise direction.
Example 1:
Input: N = 5 A[] = {1, 2, 3, 4, 5} Output: 5 1 2 3 4
Example 2:
Input: N = 8 A[] = {9, 8, 7, 6, 4, 2, 1, 3} Output: 3 9 8 7 6 4 2 1
Your Task:
You don’t need to read input or print anything. Your task is to complete the function rotate() which takes the array A[] and its size N as inputs and modify the array.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Solution :
void rotate(int arr[], int n)
{
int j = 0;
for(int i=1; i < n; i++)
{
swap(arr[0], arr[i]);
}
}
Complete Code :
// Cyclically rotate an array by one
#include <bits/stdc++.h>
using namespace std;
void rotate(int arr[], int n)
{
int j = 0;
for(int i=1; i < n; i++)
{
swap(arr[0], arr[i]);
}
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {9, 8, 7, 6, 4, 2, 1, 3};
int n = sizeof(arr)/sizeof(arr[0]);
rotate(arr, n);
return 0;
}