Given an array, the task is to cyclically rotate the array clockwise by one time.
Problem: Cyclically Rotate an Array by One
You are given an array, and the task is to rotate it by one position in a cyclic manner. That means every element will move one position to the right, and the last element will come to the first position.
Example:
- Input: [1, 2, 3, 4, 5]
- Output: [5, 1, 2, 3, 4]
Step-by-Step Explanation:
- Understand the Problem:
- You are given an array with elements. You need to rotate the elements so that each element moves one step to the right, and the last element moves to the first position. Let’s break this down:
- The element at index
i
will move to indexi+1
. - The element at the last index will move to index
0
.
- Example to Understand:
Suppose you have the array:
- Original array:
[1, 2, 3, 4, 5]
- After rotating:
[5, 1, 2, 3, 4]
What happened here? - Element
1
moved to index1
. - Element
2
moved to index2
. - Element
3
moved to index3
. - Element
4
moved to index4
. - The last element
5
moved to index0
.
- Approach to Solve the Problem:
- Step 1: Save the last element in a temporary variable.
- Step 2: Shift all other elements of the array one position to the right.
- Step 3: Place the last element (saved in the temporary variable) at the first position of the array.
Detailed Code Implementation:
Solution 1: Using a Simple Approach (In-place Modification)
1. Python:
# Function to cyclically rotate the array by one def rotate_by_one(arr): # Step 1: Store the last element in a temporary variable last_element = arr[-1] # Step 2: Shift all other elements one position to the right for i in range(len(arr)-1, 0, -1): arr[i] = arr[i-1] # Step 3: Place the last element at the first position arr[0] = last_element # Example to test arr = [1, 2, 3, 4, 5] print("Original Array:", arr) rotate_by_one(arr) print("Array after rotation:", arr)
Explanation of Python Code:
- We save the last element (
arr[-1]
) inlast_element
. - We iterate from the last index of the array to the second position (
len(arr)-1
to1
) and shift each element to the right. - Finally, we place the last element at the first position (
arr[0] = last_element
).
Output:
Original Array: [1, 2, 3, 4, 5] Array after rotation: [5, 1, 2, 3, 4]
2. C++:
#include <iostream> using namespace std; void rotate_by_one(int arr[], int n) { // Step 1: Store the last element in a temporary variable int last_element = arr[n-1]; // Step 2: Shift all other elements one position to the right for(int i = n-1; i > 0; i--) { arr[i] = arr[i-1]; } // Step 3: Place the last element at the first position arr[0] = last_element; } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Original Array: "; for(int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; rotate_by_one(arr, n); cout << "Array after rotation: "; for(int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Explanation of C++ Code:
- We store the last element (
arr[n-1]
) inlast_element
. - Using a for-loop, we shift elements to the right.
- Finally, place the last element at the first index.
Output:
Original Array: 1 2 3 4 5 Array after rotation: 5 1 2 3 4
3. Java:
import java.util.Arrays; public class RotateArray { // Function to cyclically rotate the array by one public static void rotate_by_one(int[] arr) { // Step 1: Store the last element in a temporary variable int last_element = arr[arr.length - 1]; // Step 2: Shift all other elements one position to the right for (int i = arr.length - 1; i > 0; i--) { arr[i] = arr[i - 1]; } // Step 3: Place the last element at the first position arr[0] = last_element; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("Original Array: " + Arrays.toString(arr)); rotate_by_one(arr); System.out.println("Array after rotation: " + Arrays.toString(arr)); } }
Explanation of Java Code:
- The last element is stored in
last_element
. - A for-loop shifts elements from the rightmost index to the left.
- Finally, place the last element in the first position.
Output:
Original Array: [1, 2, 3, 4, 5] Array after rotation: [5, 1, 2, 3, 4]
Solution 2: Using Collections (Python)
If you want a quick and simple solution using a built-in method, you can use list slicing.
Python Code (using slicing):
def rotate_by_one(arr): return [arr[-1]] + arr[:-1] # Example to test arr = [1, 2, 3, 4, 5] print("Original Array:", arr) arr = rotate_by_one(arr) print("Array after rotation:", arr)
This approach uses slicing:
[arr[-1]]
: Creates a list with the last element.arr[:-1]
: Gets all elements except the last.- Combining these gives the rotated array.
Output:
Original Array: [1, 2, 3, 4, 5] Array after rotation: [5, 1, 2, 3, 4]
Key Takeaways:
- In-place solution is space-efficient but may require more steps and is less intuitive.
- Slicing approach (in Python) is simple and uses extra space but is very easy to understand.
- We covered solutions in multiple languages to ensure better understanding.