Modify a matrix by rotating ith row exactly i times in clockwise direction | C ++ program for the above approach ; Function to rotate every i - th row of the matrix i times ; Traverse the matrix row - wise ; Reverse the current row ; Reverse the first i elements ; Reverse the last ( N - i ) elements ; Increment count ; Print final matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 << " STRNEWLINE " ; } } int main ( ) { vector < vector < int > > mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; rotateMatrix ( mat ) ; return 0 ; }