 # Rotate the matrix by K times | Program in C++, java, Python

Rotate the matrix by K times means rotating the given N*N matrix to the specified (K) number of times. For example, consider the 3*3 matrix, which has to be rotated once,

Matrix before Rotation :

10    20    30

40    50    60

70    80    90

Matrix after rotation :

20    30    10

50    60    40

80    90    70

Steps:

• Consider the matrix as 3 rows. Each element in the row is rotated to its right once.
• One efficient method is to consider each matrix row as an array and perform
• Copy the elements from K to end of array in a temporary array and then temporary array is made as the start of the array.
• And then the remaining elements from start to K-1 are copied in temporary array and then set to the end of the array.
• Thus, the matrix gets shifted.

## C++ program to rotate the matrix K times

`#include <iostream>#define M 3#define N 3using namespace std;// Function to print the matrixvoid displayMatrix(int matrix[][M]){for (int i = 0; i < N; i++){for (int j = 0; j < M; j++){cout << matrix[i][j] << " ";}cout << endl;}}// Main Functionint main(){int matrix[N][M];cout<<"Enter the matrix elements"<<endl;for(int i = 0 ; i < M ; i++){for(int j = 0 ; j < N ; j++){cin >> matrix[i][j]; // Input the matrix elements}}cout << "The given matrix is" << endl;displayMatrix(matrix);int temp[M];int k;cout << "Number of rotations : ";cin >> k; // input the number of rotationsk = k % M;// For rotating matrix by k timesfor (int i = 0; i < N; i++){// copy first M-k elements to temporary arrayfor (int t = 0; t < M - k; t++){temp[t] = matrix[i][t];}// copy the elements from k to end to startingfor (int j = M - k; j < M; j++){matrix[i][j - M + k] = matrix[i][j];}// copy elements from temporary array to endfor (int j = k; j < M; j++){matrix[i][j] = temp[j - k];}}cout<<"\nThe rotated matrix is\n";// display rotated matrixdisplayMatrix(matrix);return 0;}`

OUTPUT ## Java program to rotate the matrix K times

`// Java program to rotate a matrix right by k times public class Main{ // size of matrix static final int M=3; static final int N=3; // Function to rotate matrix by k times static void rotateMatrix(int matrix[][], int k) { // Temporary array of size M int temp[]=new int[M]; k = k % M; for (int i = 0; i < N; i++) { // copy first M-k elements to temporary array for (int t = 0; t < M - k; t++) temp[t] = matrix[i][t]; // copy the elements from k to end to starting for (int j = M - k; j < M; j++) matrix[i][j - M + k] = matrix[i][j]; // copy elements from temporary array to end for (int j = k; j < M; j++) matrix[i][j] = temp[j - k]; } } // Function to display the matrix static void printMatrix(int matrix[][]) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) System.out.print(matrix[i][j] + " "); System.out.println(); } } // Driver code public static void main (String[] args) { int matrix[][] = {{1,2,3} ,{4,5,6} ,{7,8,9}}; System.out.println("The inputted matrix : ");printMatrix(matrix);int k = 2; // Rotate matrix k timesrotateMatrix(matrix, k); // Print rotated matrix System.out.println("\nThe Rotated Matrix : \n");printMatrix(matrix); } }`

## Python program to rotate the matrix K times

`# Function to rotate matrix k times def rotateMatrix(k) : global M, N, matrix # Temporary array of size M temp =  * M    # Initialisation of temporary arrayk = k % M for i in range(0, N) : # copy first M-k elements to temporary array for t in range(0, M - k) : temp[t] = matrix[i][t] # copy the elements from k to end to starting for j in range(M - k, M) : matrix[i][j - M + k] = matrix[i][j] # copy elements from temporary array to end for j in range(k, M) : matrix[i][j] = temp[j - k] # function to display the matrix def printMatrix() : global M, N, matrix for i in range(0, N) : for j in range(0, M) : print ((matrix[i][j]), end = " ") print () # Main Functionk = 2     # Number of rotations# size of matrix M = 3N = 3matrix = [[1,2,3], [4,5,6], [7,8,9]] print("Initial Matrix : ")for i in range(0, N) : for j in range(0, M) : print ((matrix[i][j]), end = " ") print () # rotate matrix by k print("Rotated Matrix : ") rotateMatrix(k) # display rotated matrix printMatrix() `

### Relevant exercises

POST A NEW COMMENT

• Input (stdin)

Output (stdout)

Input (stdin)

Expected Output

Compiler Message

Input (stdin)

`2    3`

`5`
`5`
`5`