Kth diagonal from the top left of a given matrix | Function returns required diagonal ; Initialize values to prupper diagonals ; Initialize values to print lower diagonals ; Traverse the diagonals ; Print its contents ; Driver code
def printDiagonal ( K , N , M ) : NEW_LINE INDENT startrow , startcol = 0 , 0 NEW_LINE if K - 1 < N : NEW_LINE INDENT startrow = K - 1 NEW_LINE startcol = 0 NEW_LINE DEDENT else : NEW_LINE INDENT startrow = N - 1 NEW_LINE startcol = K - N NEW_LINE DEDENT while startrow >= 0 and startcol < N : NEW_LINE INDENT print ( M [ startrow ] [ startcol ] , end = " ▁ " ) NEW_LINE startrow -= 1 NEW_LINE startcol += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , K = 3 , 4 NEW_LINE M = [ [ 4 , 7 , 8 ] , [ 9 , 2 , 3 ] , [ 0 , 4 , 1 ] ] NEW_LINE printDiagonal ( K , N , M ) NEW_LINE DEDENT