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:

  1. 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 index i+1.
  • The element at the last index will move to index 0.
  1. 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 index 1.
  • Element 2 moved to index 2.
  • Element 3 moved to index 3.
  • Element 4 moved to index 4.
  • The last element 5 moved to index 0.
  1. 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:

  1. We save the last element (arr[-1]) in last_element.
  2. We iterate from the last index of the array to the second position (len(arr)-1 to 1) and shift each element to the right.
  3. Finally, we place the last element at the first position (arr[0] = last_element).
See also  array interview questions and answers

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:

  1. We store the last element (arr[n-1]) in last_element.
  2. Using a for-loop, we shift elements to the right.
  3. 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:

  1. The last element is stored in last_element.
  2. A for-loop shifts elements from the rightmost index to the left.
  3. Finally, place the last element in the first position.
See also  Given a set of points in the plane, find the smallest convex polygon that encloses all points.

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:

  1. In-place solution is space-efficient but may require more steps and is less intuitive.
  2. Slicing approach (in Python) is simple and uses extra space but is very easy to understand.
  3. We covered solutions in multiple languages to ensure better understanding.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *