Given an array, rotate the array by one position in a clock-wise direction.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<=N<=105
0<=a[i]<=105
//Here we have a full program for rotate by 1
1: # include <iostream>
2: using namespace std;
3: void rotate(int arr[], int n)
4: {
5: int x = arr[n - 1], i;
6: for (i = n - 1; i > 0; i--)
7: arr[i] = arr[i - 1];
8: arr[0] = x;
9: }
10: // Driver code
11: int main()
12: {
13: int arr[] = {1, 2, 3, 4, 5}, i;
14: int n = sizeof(arr) /
15: sizeof(arr[0]);
16: cout << "Given array is \n";
17: for (i = 0; i < n; i++)
18: cout << arr[i];
19: rotate(arr, n);
20: cout << "\nRotated array is\n";
21: for (i = 0; i < n; i++)
22: cout << arr[i];
23: return 0;
24: }
0 Comments
If you have any doubt then let me know.