Perform right rotation on a matrix by k times in C++

In this tutorial, we will learn how to perform right rotation on a matrix by k times in C++ with a solved example, algorithm.

What is the meaning of “perform right rotation on a matrix”? Well, right rotation or we can say the right shift on the matrix is shifting each column to the right side. And k times means performing this rotation k times.

How to perform right rotation on  a matrix by k times

It is simple to perform right rotation on a matrix by k times, we just have to shift each column of matrix right side one by one by k times. Lett’s take an example and perform a right rotation in k times manually.

Example:

 Input matrix: 
                  1 2 3 4 
                  6 7 8 9 
                  0 9 8 7 
                  5 4 3 2 
 k = 2         

before rotation
column-1 elements: 1 6 0 5
column-2 elements: 2 7 9 4
column-3 elements: 3 8 8 3
column-4 elements: 4 9 7 2

performing right rotation 1st time i.e k = 1, we get: 
               
                 4 1 2 3
                 9 6 7 8
                 7 0 9 8
                 2 5 4 3

After first right rotation: 
column-1 elements: 4 9 7 2
column-2 elements: 1 6 0 5
column-3 elements: 2 7 9 4
column-4 elements: 3 8 8 3

After performing right rotation 2nd time i.e. k = 2, we get:

                 3 4 1 2
                 8 9 6 7
                 8 7 0 9
                 3 2 5 4 

After second right rotation: 
column-1 elements: 3 8 8 3
column-2 elements: 4 9 7 2
column-3 elements: 1 6 0 5
column-4 elements: 2 7 9 4

Therefor, after performing right rotation by given k times i.e. by 2 times we
get the matrix: 
                 3 4 1 2
                 8 9 6 7
                 8 7 0 9
                 3 2 5 4

Approach: 

The approach to performing rotation k times is simple to understand but a little bit tricky to implement. In the approach, first, we will copy each column element of each ith row to a  temporary array till m – k. after that we will copy the elements from k to end to start off each ith row, then again we will copy back each element from temporary array to end of each ith row of the matrix.

Let’s see the algorithm for the approach that we are going to follow.

Algorithm to perform right rotation on a matrix by k times

  1. Declare and initialize a matrix of size m*n.
  2. Take a number as input for the k right rotation. (consider the number saved to variable k)
  3. Print the elements of the array before performing rotation.
  4. Save the k % m into k for effective right rotation.
  5. Initialize a temporary array of size equal to the column of the matrix. (consider temporary array be temp[])
  6. Start an outer loop from i = 0 to n (size of the row). Inside the outer loop start another loop form t = 0 to m – k and for each t, copy matrix[i][t] into temp[t] to copy first m – k column elements to temp.
  7. Start another inner loop from j = m – k to m and save matrix[i][j] to matrix[i][i – m + k] to copy the elements from k to end to starting.
  8. Start the third inner loop from j =k to m and copy back the elements fo temp[j – k] to matrix[i][j].
  9. Display the elements of the matrix.

C++ program to perform right rotation on a matrix by k times

#include <iostream> 
using namespace std; 
  
void rotate_matrix_K_times(int matrix[50][50], int m, int n, int k);
void display(int matrix[50][50], int m, int n);

int main() { 
  
  int matrix[50][50], m, n, k;

  cout<<"Enter the length of the row of matrix: ";
  cin>>m;
  cout<<"Enter the length of the column of matrix: ";
  cin>>n;
  cout<<"\nInput the elements in the matrix: \n";
  for(int i = 0; i < m; i++)
    for(int j = 0; j < n; j++)
        cin>>matrix[i][j];

  cout<<"Enter the value of \"k\" to rotate matrix k times: ";
  cin>>k;

  cout<<"\nMatrix before performing rotation by k times: \n";
  display(matrix, m, n); 
  
  // rotate matrix by k 
  rotate_matrix_K_times(matrix, m, n, k); 
  
  cout<<"\nMatrix after performing rotation by k times: \n";
  display(matrix, m, n); 
  
  return 0; 
} 

// function to rotate matrix by k times 
void rotate_matrix_K_times(int matrix[50][50], int m, int n, int k) { 

  //A temporary array of size m 
  int temp[m]; 
  
  //if we get the value of k more than or equal to m
  //then we have to reduce m from the km as it will result the same 
  //matrix
  //after each m rotation. 
  //therefore we need only k % m effective rotations to perform.
  k = k % m; 
  
  //main nasted iteration to rotate matrix k times
  for (int i = 0; i < n; i++) {

    //copyig first m-k column elements to array temp for each ith row 
    for (int t = 0; t < m - k; t++) 
        temp[t] = matrix[i][t]; 
  
    //copying the elements from k to end to starting of each ith row 
    for (int j = m - k; j < m; j++) 
        matrix[i][j - m + k] = matrix[i][j]; 
  
    //copying each elements from temporary array to end of each ith row 
    for (int j = k; j < m; j++) 
        matrix[i][j] = temp[j - k]; 
  } 
} 
  
//function to display the elements of array.
void display(int matrix[50][50], int m, int n) { 

  for (int i = 0; i < n; i++) { 
    for (int j = 0; j < m; j++) 
      cout << matrix[i][j] << " "; 
    cout << endl; 
  } 
} 

Output: 

Enter the length of the row of matrix: 4
Enter the length of the column of matrix: 4

Input the elements in the matrix:
1 2 3 4
6 7 8 9
0 9 8 7
5 4 3 2
Enter the value of "k" to rotate matrix k times: 2

Matrix before performing rotation by k times:
1 2 3 4
6 7 8 9
0 9 8 7
5 4 3 2

Matrix after performing rotation by k times:
3 4 1 2
8 9 6 7
8 7 0 9
3 2 5 4

 

Time Complexity: O(n*m), where n and m are the lengths of row and column of matrix respectively.

By this, you can perform right rotation on a matrix by k times.

You may also read:

  1. How to swap both diagonals of a matrix in C++
  2. How to find transpose of a matrix in C++
  3. Find sum of each row and column of a matrix in C++

Leave a Reply

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