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

10 min read

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 array rotation.
  • 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 3
using namespace std;

// Function to print the matrix
void 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 Function
int 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 rotations
k = k % M;

// For rotating matrix by k times

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];
}}

cout<<"\nThe rotated matrix is\n";
// display rotated matrix

displayMatrix(matrix);
return 0;
}

 

OUTPUT

rotate the matrix by k times

 

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 times
rotateMatrix(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 = [0] * M # Initialisation of temporary array
k = 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 Function
k = 2 # Number of rotations

# size of matrix 
M = 3
N = 3
matrix = [[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() 
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error