Rotate all Matrix elements except the diagonal K times by 90 degrees in clockwise direction
Last Updated :
20 Jul, 2022
Given a square matrix mat[][] of dimension N and an integer K, the task is to rotate the matrix by 90 degrees K times without changing the position of the diagonal elements.
Examples:
Input: mat[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}}, K = 1
Output:
1 16 11 6 5
22 7 12 9 2
23 18 13 8 3
24 17 14 19 4
21 20 15 10 25
Input: mat[][] = {{10, 11}, {12, 13}}, K = 2
Output:
10 11
12 13
Approach: The given problem can be solved by using the idea discussed in this article and the fact that the matrix restores after performing clockwise rotation 4 times. Follow the below steps to solve the given problem:
- Update the value of K as K % 4.
- Iterate until K is a positive and perform the following steps:
- Traverse the matrix, for i over the range [0, N / 2) and j over the range[0, N – i – 1) and perform the following steps:
- If the value of i != j and (i + j) != (N – 1), then perform the following steps:
- Store the value of mat[i][j] in a temporary variable temp.
- Update the value of mat[i][j] as mat[N – 1 – j][i].
- Update the value of mat[N – 1 – j][i] as mat[N – 1 -i][N – 1 – j].
- Update the value of mat[N – 1 – i][N – 1 – j] as mat[j][N – 1 – i].
- Update the value of mat[j][N – 1 – i] as temp.
- After completing the above steps, print the updated matrix obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector< int > >& mat)
{
for ( int i = 0; i < mat.size(); i++) {
for ( int j = 0; j < mat[0].size(); j++)
cout << setw(3) << mat[i][j];
cout << "\n" ;
}
}
void performSwap(vector<vector< int > >& mat,
int i, int j)
{
int N = mat.size();
int ei = N - 1 - i;
int ej = N - 1 - j;
int temp = mat[i][j];
mat[i][j] = mat[ej][i];
mat[ej][i] = mat[ei][ej];
mat[ei][ej] = mat[j][ei];
mat[j][ei] = temp;
}
void rotate(vector<vector< int > >& mat,
int N, int K)
{
K = K % 4;
while (K--) {
for ( int i = 0; i < N / 2; i++) {
for ( int j = i;
j < N - i - 1; j++) {
if (i != j
&& (i + j) != N - 1) {
performSwap(mat, i, j);
}
}
}
}
print(mat);
}
int main()
{
int K = 5;
vector<vector< int > > mat = {
{ 1, 2, 3, 4 },
{ 6, 7, 8, 9 },
{ 11, 12, 13, 14 },
{ 16, 17, 18, 19 },
};
int N = mat.size();
rotate(mat, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static void print( int mat[][])
{
for ( int i = 0 ; i < mat.length; i++) {
for ( int j = 0 ; j < mat[ 0 ].length; j++)
System.out.print(mat[i][j] + " " );
System.out.println();
}
}
static void performSwap( int mat[][], int i, int j)
{
int N = mat.length;
int ei = N - 1 - i;
int ej = N - 1 - j;
int temp = mat[i][j];
mat[i][j] = mat[ej][i];
mat[ej][i] = mat[ei][ej];
mat[ei][ej] = mat[j][ei];
mat[j][ei] = temp;
}
static void rotate( int mat[][], int N, int K)
{
K = K % 4 ;
while (K-- > 0 ) {
for ( int i = 0 ; i < N / 2 ; i++) {
for ( int j = i; j < N - i - 1 ; j++) {
if (i != j && (i + j) != N - 1 ) {
performSwap(mat, i, j);
}
}
}
}
print(mat);
}
public static void main(String[] args)
{
int K = 5 ;
int mat[][] = {
{ 1 , 2 , 3 , 4 },
{ 6 , 7 , 8 , 9 },
{ 11 , 12 , 13 , 14 },
{ 16 , 17 , 18 , 19 },
};
int N = mat.length;
rotate(mat, N, K);
}
}
|
Python3
def printMat(mat):
for i in range ( len (mat)):
for j in range ( len (mat[ 0 ])):
print (mat[i][j], end = " " )
print ()
def performSwap(mat, i, j):
N = len (mat)
ei = N - 1 - i
ej = N - 1 - j
temp = mat[i][j]
mat[i][j] = mat[ej][i]
mat[ej][i] = mat[ei][ej]
mat[ei][ej] = mat[j][ei]
mat[j][ei] = temp
def rotate(mat, N, K):
K = K % 4
while (K > 0 ):
for i in range ( int (N / 2 )):
for j in range (i, N - i - 1 ):
if (i ! = j and (i + j) ! = N - 1 ):
performSwap(mat, i, j)
K - = 1
printMat(mat)
K = 5
mat = [ [ 1 , 2 , 3 , 4 ],
[ 6 , 7 , 8 , 9 ],
[ 11 , 12 , 13 , 14 ],
[ 16 , 17 , 18 , 19 ] ]
N = len (mat)
rotate(mat, N, K)
|
C#
using System;
public class GFG {
static void print( int [, ] mat)
{
for ( int i = 0; i < mat.GetLength(0); i++) {
for ( int j = 0; j < mat.GetLength(1); j++)
Console.Write(mat[i, j] + " " );
Console.WriteLine();
}
}
static void performSwap( int [, ] mat, int i, int j)
{
int N = mat.GetLength(0);
int ei = N - 1 - i;
int ej = N - 1 - j;
int temp = mat[i, j];
mat[i, j] = mat[ej, i];
mat[ej, i] = mat[ei, ej];
mat[ei, ej] = mat[j, ei];
mat[j, ei] = temp;
}
static void rotate( int [, ] mat, int N, int K)
{
K = K % 4;
while (K-- > 0) {
for ( int i = 0; i < N / 2; i++) {
for ( int j = i; j < N - i - 1; j++) {
if (i != j && (i + j) != N - 1) {
performSwap(mat, i, j);
}
}
}
}
print(mat);
}
public static void Main( string [] args)
{
int K = 5;
int [, ] mat = {
{ 1, 2, 3, 4 },
{ 6, 7, 8, 9 },
{ 11, 12, 13, 14 },
{ 16, 17, 18, 19 },
};
int N = mat.GetLength(0);
rotate(mat, N, K);
}
}
|
Javascript
<script>
function print(mat)
{
for (let i = 0; i < mat.length; i++) {
for (let j = 0; j < mat[0].length; j++)
document.write(mat[i][j] + " " );
document.write( "<br/>" );
}
}
function performSwap(mat, i, j)
{
let N = mat.length;
let ei = N - 1 - i;
let ej = N - 1 - j;
let temp = mat[i][j];
mat[i][j] = mat[ej][i];
mat[ej][i] = mat[ei][ej];
mat[ei][ej] = mat[j][ei];
mat[j][ei] = temp;
}
function rotate(mat, N, K)
{
K = K % 4;
while (K-- > 0) {
for (let i = 0; i < N / 2; i++) {
for (let j = i; j < N - i - 1; j++) {
if (i != j && (i + j) != N - 1) {
performSwap(mat, i, j);
}
}
}
}
print(mat);
}
let K = 5;
let mat = [
[ 1, 2, 3, 4 ],
[ 6, 7, 8, 9 ],
[ 11, 12, 13, 14 ],
[ 16, 17, 18, 19 ],
];
let N = mat.length;
rotate(mat, N, K);
</script>
|
Output:
1 11 6 4
17 7 8 2
18 12 13 3
16 14 9 19
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...