To write a program to provided implements the cyclically rotate of an array by one position. Cyclic rotation involves moving each element of the array one position to the right, with the last element moving to the first position. This operation can be useful in various scenarios, such as shifting elements in a circular manner or creating a new arrangement of elements.
Example to Write a program to cyclically rotate an array by one
Let’s go through the code step by step:
- The
rotateArray
method takes an integer arrayarr
as input and performs the cyclic rotation by one position. - If the array has 0 or 1 element, no rotation is needed, so we simply return from the method.
- We store the last element of the array in a variable called
lastElement
. - Starting from the last index, we iterate through the array in reverse order using a
for
loop. - In each iteration, we shift the current element to the right by assigning the value of the previous element to the current element.
- After the loop, we set the first element of the array (
arr[0]
) to thelastElement
, completing the cyclic rotation. - In the
main
method, we create an example arrayarr
and print its original state. - We call the
rotateArray
method, passing the array as an argument, to perform the rotation. - Finally, we print the modified array to verify the result.
import java.util.Arrays;
public class CyclicRotation {
public static void rotateArray(int[] arr) {
if (arr.length <= 1) {
return; // No rotation needed for arrays of size 0 or 1
}
int lastElement = arr[arr.length - 1];
for (int i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = lastElement;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Original array: " + Arrays.toString(arr));
rotateArray(arr);
System.out.println("Array after rotation: " + Arrays.toString(arr));
}
}
Output:
Original array: [1, 2, 3, 4, 5]
Array after rotation: [5, 1, 2, 3, 4]
- Time Complexity: The time complexity of the program is O(n), where n is the size of the array. This is because the program performs a single pass through the array, iterating over each element once to shift them to their new positions. The time complexity grows linearly with the size of the array.
- Space Complexity: The space complexity of the program is O(1), which means it uses a constant amount of additional space regardless of the size of the array. The rotation is performed in-place, modifying the original array without requiring any additional data structures. Therefore, the space complexity remains constant.
Suggestions:
Factorial of a Number Full Explanation With Examples
Check Tree is a BST or not Full Explanation
Postorder Traversal in Binary Search Tree With Full Explanation
Preorder Traversal In Binary Search Tree Full Explanation With Example
Inorder Traversal in Binary Search Tree Full Explanation
Binary Search Tree Traversal With Full Examples And Explanation
Difference Between Binary Tree and Binary Search Tree With Complete Examples
Searching in Binary Search Tree With Full Explanation
Binary Search Tree With Full Explanation And Examples
Minimize the maximum difference between heights
Write a program to cyclically rotate an array by one
Find the Union and Intersection of the two sorted arrays
Move all the negative elements to one side of the array
Next Permutation in c++ || FlutterTPoint With examples
Kth max and min element of an array
Kadaneโs Algorithm In JavaScript With Example
Find the Maximum and Minimum element in an Array
Sorting An Array In JavaScript