Check if two elements of a matrix are on the same diagonal or not | Function to check if two integers are on the same diagonal of the matrix ; Storing Indexes of y in P , Q ; Condition to check if the both the elements are in same diagonal of a matrix ; Driver Code ; Dimensions of Matrix ; Given Matrix ; elements to be checked ; Function Call
def checkSameDiag ( x , y ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if li [ i ] [ j ] == x : NEW_LINE INDENT I , J = i , j NEW_LINE DEDENT if li [ i ] [ j ] == y : NEW_LINE INDENT P , Q = i , j NEW_LINE DEDENT DEDENT DEDENT if P - Q == I - J or P + Q == I + J : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m , n = 6 , 5 NEW_LINE li = [ [ 32 , 94 , 99 , 26 , 82 ] , [ 51 , 69 , 52 , 63 , 17 ] , [ 90 , 36 , 88 , 55 , 33 ] , [ 93 , 42 , 73 , 39 , 28 ] , [ 81 , 31 , 83 , 53 , 10 ] , [ 12 , 29 , 85 , 80 , 87 ] ] NEW_LINE x , y = 42 , 80 NEW_LINE checkSameDiag ( x , y ) NEW_LINE DEDENT