ADVERTISEMENT
ADVERTISEMENT

Program to Cyclically Rotate an Array by One

C++ Implementation for cyclically rotate an array by one.
Submitted by Vikneshwar GK, on February 16, 2022

Array Rotation

Consider an integer array of size n. The task at hand is to rotate the elements clockwise by one element, i.e., place the last elements at the start of the array by pushing the remaining elements to the end.

Example:

Input:
array[]= {1, 2, 3, 4, 5}
            
Output:
array[] = {5, 1, 2, 3, 4}

Input:
array[]= {10, 20, 30, 40, 50}

Output:
array[]= {50, 10, 20, 30, 40}

Solution 1: Using Temporary Variable

This is a simple solution that involves a temporary variable to store the last element. It involves the following steps:

  • Initialize a temporary variable with the last element of the array
  • Iterate the array till n-1 and move the elements to their next position
  • Store the temporary element in array[0]
  • Print the array

C++ Implementation:

#include <iostream>
using namespace std;

// function to perform clockwise rotation
void rotate(int array[], int length)
{
    int temp = array[length - 1], i;
    for (i = length - 1; i > 0; i--)
        array[i] = array[i - 1];
    array[0] = temp;
}

//Function to print the array
void printArray(int array[], int length)
{
    for (int i = 0; i < length; i++)
        cout << array[i] << " ";
    cout << endl;
}

// Main function
int main()
{
    int array[100], N, d;
    
    cout << "Enter Number of elements: ";
    cin >> N;

    for (int i = 0; i < N; i++) {
        cout << "Enter element " << i + 1 << ":";
        cin >> array[i];
    }
    
    rotate(array, N);
    cout << "Rotated Array" << endl;
    printArray(array, N);

    return 0;
}

Output:

Enter Number of elements: 5
Enter element 1:5
Enter element 2:4
Enter element 3:3
Enter element 4:2
Enter element 5:1
Rotated Array
1 5 4 3 2

Time Complexity: O(n), where n is the length of the array.

Solution 2: Using Pointers

Since the problem needs the last element to be placed at first, we can use 2 pointers that point to the first and last position of the array and start swapping. It involves the following steps:

  • Assign start with array[0] and end with array[n-1]
  • Swap start and end pointers values in the array
  • Keeping end pointer fixed, increment the start pointer, and continue swapping
  • Perform the above steps till the start is not equal to the end
  • Print the array

C++ Implementation:

#include <iostream>
using namespace std;

// function to perform clockwise rotation
void rotate(int array[], int length)
{
    int start = 0, end = length - 1;
    while (start != end) {
        swap(array[start], array[end]);
        start++;
    }
}

//Function to print the array
void printArray(int array[], int length)
{
    for (int i = 0; i < length; i++)
        cout << array[i] << " ";
    cout << endl;
}

// Main function
int main()
{
    int array[100], N, d;
    
    cout << "Enter Number of elements: ";
    cin >> N;

    for (int i = 0; i < N; i++) {
        cout << "Enter element " << i + 1 << ":";
        cin >> array[i];
    }
    rotate(array, N);
    cout << "Rotated Array" << endl;
    printArray(array, N);

    return 0;
}

Output:

Enter Number of elements: 5
Enter element 1:5
Enter element 2:6
Enter element 3:7
Enter element 4:8
Enter element 5:9
Rotated Array
9 5 6 7 8 

Time Complexity: O(n), where n is the length of the array.

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.