Rotate the matrix right by K times | size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver code ; rotate matrix by k ; display rotated matrix
< ? php $ M = 3 ; $ N = 3 ; function rotateMatrix ( & $ matrix , $ k ) { global $ M , $ N ; $ temp = array ( ) ; $ k = $ k % $ M ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ t = 0 ; $ t < $ M - $ k ; $ t ++ ) $ temp [ $ t ] = $ matrix [ $ i ] [ $ t ] ; for ( $ j = $ M - $ k ; $ j < $ M ; $ j ++ ) $ matrix [ $ i ] [ $ j - $ M + $ k ] = $ matrix [ $ i ] [ $ j ] ; for ( $ j = $ k ; $ j < $ M ; $ j ++ ) $ matrix [ $ i ] [ $ j ] = $ temp [ $ j - $ k ] ; } } function displayMatrix ( & $ matrix ) { global $ M , $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) echo ( $ matrix [ $ i ] [ $ j ] . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } } $ matrix = array ( array ( 12 , 23 , 34 ) , array ( 45 , 56 , 67 ) , array ( 78 , 89 , 91 ) ) ; $ k = 2 ; rotateMatrix ( $ matrix , $ k ) ; displayMatrix ( $ matrix ) ; ? >