Rotate all Matrix elements except the diagonal K times by 90 degrees in clockwise direction | Function to print the matrix ; Iterate over the rows ; Iterate over the columns ; Print the value ; Function to perform the swapping of matrix elements in clockwise manner ; Stores the last row ; Stores the last column ; Perform the swaps ; Function to rotate non - diagonal elements of the matrix K times in clockwise direction ; Update K to K % 4 ; Iterate until K is positive ; Iterate each up to N / 2 - th row ; Iterate each column from i to N - i - 1 ; Check if the element at i , j is not a diagonal element ; Perform the swapping ; Print the matrix ; Driver Code
def printMat ( mat ) : NEW_LINE INDENT for i in range ( len ( mat ) ) : NEW_LINE INDENT for j in range ( len ( mat [ 0 ] ) ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def performSwap ( mat , i , j ) : NEW_LINE INDENT N = len ( mat ) NEW_LINE ei = N - 1 - i NEW_LINE ej = N - 1 - j NEW_LINE temp = mat [ i ] [ j ] NEW_LINE mat [ i ] [ j ] = mat [ ej ] [ i ] NEW_LINE mat [ ej ] [ i ] = mat [ ei ] [ ej ] NEW_LINE mat [ ei ] [ ej ] = mat [ j ] [ ei ] NEW_LINE mat [ j ] [ ei ] = temp NEW_LINE DEDENT def rotate ( mat , N , K ) : NEW_LINE INDENT K = K % 4 NEW_LINE while ( K > 0 ) : NEW_LINE INDENT for i in range ( int ( N / 2 ) ) : NEW_LINE INDENT for j in range ( i , N - i - 1 ) : NEW_LINE INDENT if ( i != j and ( i + j ) != N - 1 ) : NEW_LINE INDENT performSwap ( mat , i , j ) NEW_LINE DEDENT DEDENT DEDENT K -= 1 NEW_LINE DEDENT printMat ( mat ) NEW_LINE DEDENT K = 5 NEW_LINE mat = [ [ 1 , 2 , 3 , 4 ] , [ 6 , 7 , 8 , 9 ] , [ 11 , 12 , 13 , 14 ] , [ 16 , 17 , 18 , 19 ] ] NEW_LINE N = len ( mat ) NEW_LINE rotate ( mat , N , K ) NEW_LINE