Print K 'th element in spiral form of matrix |  ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; check the first row from the remaining rows ; check the last column from the remaining columns ; check the last row from the remaining rows ; check the first column from the remaining columns ; Driver program to test above functions
R = 3 NEW_LINE C = 6 NEW_LINE def spiralPrint ( m , n , a , c ) : NEW_LINE INDENT k = 0 NEW_LINE l = 0 NEW_LINE count = 0 NEW_LINE while ( k < m and l < n ) : NEW_LINE INDENT for i in range ( l , n ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count == c ) : NEW_LINE INDENT print ( a [ k ] [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT k += 1 NEW_LINE for i in range ( k , m ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count == c ) : NEW_LINE INDENT print ( a [ i ] [ n - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT n -= 1 NEW_LINE if ( k < m ) : NEW_LINE INDENT for i in range ( n - 1 , l - 1 , - 1 ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count == c ) : NEW_LINE INDENT print ( a [ m - 1 ] [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT m -= 1 NEW_LINE DEDENT if ( l < n ) : NEW_LINE INDENT for i in range ( m - 1 , k - 1 , - 1 ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count == c ) : NEW_LINE INDENT print ( a [ i ] [ l ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT l += 1 NEW_LINE DEDENT DEDENT DEDENT a = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] ] NEW_LINE k = 17 NEW_LINE spiralPrint ( R , C , a , k ) NEW_LINE