Modify a matrix by rotating ith row exactly i times in clockwise direction
Given a matrix mat[][] of dimensions M * N, the task is to print the matrix obtained after rotating every ith row of the matrix i times in a clockwise direction.
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output:
1 2 3
6 4 5
8 9 7
Explanation:
The 0th row is rotated 0 times. Therefore, the 0th row remains the same as {1, 2, 3}.
The 1st row is rotated 1 times. Therefore, the 1st row modifies to {6, 4, 5}.
The 2nd row is rotated 2 times. Therefore, the 2nd row modifies to {8, 9, 7}.
After completing the above operations, the given matrix modifies to {{1, 2, 3}, {6, 4, 5}, {8, 9, 7}}.
Input: mat[][] = {{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 8}, {7, 8, 9, 8}}
Output:
1 2 3 4
7 4 5 6
9 8 7 8
8 9 8 7
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rotateMatrix(vector<vector< int > >& mat)
{
int i = 0;
for ( auto & it : mat) {
reverse(it.begin(), it.end());
reverse(it.begin(), it.begin() + i);
reverse(it.begin() + i, it.end());
i++;
}
for ( auto rows : mat) {
for ( auto cols : rows) {
cout << cols << " " ;
}
cout << "\n" ;
}
}
int main()
{
vector<vector< int > > mat
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
rotateMatrix(mat);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void reverse( int arr[], int start, int end)
{
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void rotateMatrix( int mat[][])
{
int i = 0 ;
for ( int rows[] : mat) {
reverse(rows, 0 , rows.length - 1 );
reverse(rows, 0 , i - 1 );
reverse(rows, i, rows.length - 1 );
i++;
}
for ( int rows[] : mat) {
for ( int cols : rows) {
System.out.print(cols + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int mat[][] = { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
rotateMatrix(mat);
}
}
|
Python3
def rotateMatrix(mat):
i = 0
mat1 = []
for it in mat:
it.reverse()
it1 = it[:i]
it1.reverse()
it2 = it[i:]
it2.reverse()
i + = 1
mat1.append(it1 + it2)
for rows in mat1:
for cols in rows:
print (cols, end = " " )
print ()
if __name__ = = "__main__" :
mat = [ [ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ] ]
rotateMatrix(mat)
|
C#
using System;
class GFG
{
static void reverse( int N, int [, ] mat, int start, int end)
{
while (start < end) {
int temp = mat[N,start];
mat[N, start] = mat[N, end];
mat[N, end] = temp;
start++;
end--;
}
}
static void rotateMatrix( int N, int [, ] mat)
{
int i = 0;
for ( int j = 0; j < N; j++)
{
reverse(j, mat, 0, N - 1);
reverse(j, mat, 0, i - 1);
reverse(j, mat, i, N - 1);
i++;
}
for (i = 0; i < N; i++) {
for ( int j = 0; j < N; j++)
Console.Write(mat[i, j] + " " );
Console.Write( "\n" );
}
}
static public void Main()
{
int N = 3;
int [, ] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }};
rotateMatrix(N, mat);
}
}
|
Javascript
<script>
function reverse(arr,start,end)
{
while (start < end) {
let temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
function rotateMatrix(mat)
{
let i = 0;
for (let rows=0;rows<mat.length;rows++) {
reverse(mat[rows], 0, mat[rows].length - 1);
reverse(mat[rows], 0, i - 1);
reverse(mat[rows], i, mat[rows].length - 1);
i++;
}
for (let rows=0;rows< mat.length;rows++) {
for (let cols=0;cols< mat[rows].length;cols++) {
document.write(mat[rows][cols] + " " );
}
document.write( "<br>" );
}
}
let mat=[[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]];
rotateMatrix(mat);
</script>
|
Output:
1 2 3
6 4 5
8 9 7
Time Complexity: O(M * N)
Auxiliary Space: O(1)
Last Updated :
07 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...