Write a program to cyclically rotate an array by one

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:

  1. The rotateArray method takes an integer array arr as input and performs the cyclic rotation by one position.
  2. If the array has 0 or 1 element, no rotation is needed, so we simply return from the method.
  3. We store the last element of the array in a variable called lastElement.
  4. Starting from the last index, we iterate through the array in reverse order using a for loop.
  5. In each iteration, we shift the current element to the right by assigning the value of the previous element to the current element.
  6. After the loop, we set the first element of the array (arr[0]) to the lastElement, completing the cyclic rotation.
  7. In the main method, we create an example array arr and print its original state.
  8. We call the rotateArray method, passing the array as an argument, to perform the rotation.
  9. 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]
  1. 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.
  2. 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

How to reverse an array

Find the Maximum and Minimum element in an Array

Sorting An Array In JavaScript

Donโ€™t miss new tips!

We donโ€™t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment

Translate ยป
Scroll to Top