Modify a matrix by rotating ith row exactly i times in clockwise direction | Function to reverse arr [ ] from start to end ; 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
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 ( " " ) ; } } let mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; rotateMatrix ( mat ) ;