Cyclically Rotate an Array by One
- Shreyas Naphad
- Jul 24, 2024
- 1 min read
In this article, we will solve the problem of cyclically rotating an array by one position to the right.
Problem Statement: Given an array of integers, write a function to cyclically rotate the array by one position to the right. The last element of the array should become the first element, and all other elements should be shifted one position to the right.
Example:
Input: [0, 2, 4, 10, 5]
Output: [5, 0, 2, 4, 10]
Solution:
To cyclically rotate an array by one, we can follow these steps:
1. Store the last element of the array in a temporary variable.
2. Shift all other elements of the array one position to the right.
3. Place the last element (stored in the temporary variable) at the first position of the array.
Time Complexity: O(N)
Space Complexity: O(1)
Comments