Wednesday, 27 Oct 2021

# 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 36 4 58 9 7Explanation: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 47 4 5 69 8 7 88 9 8 7

Approach: Follow the steps below to solve the problem:Below is the implementation of the above approach:

C++

#include
using namespace std;

void rotateMatrix(vector& 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