Check if two elements of a matrix are on the same diagonal or not | C ++ program for the above approach ; Function to check if two integers are on the same diagonal of the matrix ; Storing indexes of x in I , J ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkSameDiag ( int li [ 6 ] [ 5 ] , int x , int y , int m , int n ) { int I = 0 , J = 0 ; int P = 0 , Q = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( li [ i ] [ j ] == x ) { I = i ; J = j ; } if ( li [ i ] [ j ] == y ) { P = i ; Q = j ; } } } if ( P - Q == I - J P + Q == I + J ) { cout << " YES " ; } else cout << " NO " ; } int main ( ) { int m = 6 ; int n = 5 ; int li [ 6 ] [ 5 ] = { { 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 } } ; int x = 42 ; int y = 80 ; checkSameDiag ( li , x , y , m , n ) ; return 0 ; }