Check if two elements of a matrix are on the same diagonal or not - GeeksforGeeks

26 Nov, 2021
Given a matrix mat[][], and two integers X and Y, the task is to check if X and Y are on the same diagonal of the given matrix or not.
Examples:
Input: mat[][]= {{1, 2}. {3, 4}}, X = 1, Y = 4 Output: YesExplanation:Both X and Y lie on the same diagonal.
Input: mat[][]= {{1, 2}. {3, 4}}, X = 2, Y = 4Output: No
Approach: The key observation to solve the problem is that the two elements of the matrix are on the same diagonal only if the sum of the indices or the absolute difference of the indices of the elements are equal. Follow the steps below to solve the problem:
Traverse the matrix and find the indices of the elements of the matrix.
Check if the elements are on the same diagonal.Let the indices of the elements of the matrix are (P, Q) and (X, Y), then the condition that both the elements are on the same diagonal is given by the following equation:
P – Q == X – Y or P + Q == X + Y
If the above condition is satisfied, print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for// the above approach#include <bits/stdc++.h>using namespace std; // Function to check if two// integers are on the same// diagonal of the matrixvoid checkSameDiag(int li[6][5], int x,                   int y, int m, int n){  // Storing indexes of x in I, J  int I = 0, J = 0;   // Storing Indexes of y in P, Q  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;      }    }  }   // Condition to check if the  // both the elements are in  // same diagonal of a matrix  if (P - Q == I - J ||      P + Q == I + J)  {    cout << "YES";  }  else    cout << "NO";} // Driver codeint main(){  // Dimensions of Matrix  int m = 6;  int n = 5;   // Given Matrix  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}};   // Elements to be checked  int x = 42;  int y = 80;   // Function call  checkSameDiag(li, x, y, m, n);  return 0;} //This code is contributed by 29AjayKumar
// Java implementation of the// above approachclass GFG{     // Function to check if two// integers are on the same// diagonal of the matrixstatic void checkSameDiag(int li[][], int x,                          int y, int m, int n){         // Storing indexes of x in I, J    int I = 0, J = 0;         // Storing Indexes of y in P, Q    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;            }        }    }         // Condition to check if the    // both the elements are in    // same diagonal of a matrix    if (P - Q == I - J ||        P + Q == I + J)    {        System.out.println("YES");    }    else        System.out.println("NO");} // Driver Codepublic static void main(String[] args){         // Dimensions of Matrix    int m = 6;    int n = 5;     // Given Matrix    int[][] 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 } };     // Elements to be checked    int x = 42;    int y = 80;     // Function call    checkSameDiag(li, x, y, m, n);}} // This code is contributed by Amit Katiyar
# Python3 implementation of the# above approach # Function to check if two# integers are on the same# diagonal of the matrixdef checkSameDiag(x, y):         # Storing indexes of x in I, J    # Storing Indexes of y in P, Q    for i in range(m):        for j in range(n):            if li[i][j] == x:                I, J = i, j            if li[i][j] == y:                P, Q = i, j                     # Condition to check if the    # both the elements are in    # same diagonal of a matrix    if P-Q == I-J or P + Q == I + J:        print("YES")    else:        print("NO")  # Driver Codeif __name__ == "__main__":         # Dimensions of Matrix    m, n = 6, 5         # Given Matrix    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]]         # elements to be checked    x, y = 42, 80         # Function Call    checkSameDiag(x, y)
// C# implementation of the// above approachusing System;class GFG{     // Function to check if two// integers are on the same// diagonal of the matrixstatic void checkSameDiag(int [,]li, int x,                          int y, int m, int n){  // Storing indexes of x in I, J  int I = 0, J = 0;   // Storing Indexes of y in P, Q  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;      }    }  }   // Condition to check if the  // both the elements are in  // same diagonal of a matrix  if (P - Q == I - J ||                         P + Q == I + J)  {    Console.WriteLine("YES");  }  else    Console.WriteLine("NO");} // Driver Codepublic static void Main(String[] args){  // Dimensions of Matrix  int m = 6;  int n = 5;   // Given Matrix  int [,]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}};   // Elements to be checked  int x = 42;  int y = 80;   // Function call  checkSameDiag(li, x, y, m, n);}} // This code is contributed by 29AjayKumar
<script> // JavaScript program for// the above approach   // Function to check if two// integers are on the same// diagonal of the matrixfunction checkSameDiag(li, x,                          y, m, n){          // Storing indexes of x in I, J    let I = 0, J = 0;          // Storing Indexes of y in P, Q    let P = 0, Q = 0;          for(let i = 0; i < m; i++)    {        for(let j = 0; j < n; j++)        {            if (li[i][j] == x)            {                I = i;                J = j;            }            if (li[i][j] == y)            {                P = i;                Q = j;            }        }    }          // Condition to check if the    // both the elements are in    // same diagonal of a matrix    if (P - Q == I - J ||        P + Q == I + J)    {        document.write("YES");    }    else        document.write("NO");} // Driver code     // Dimensions of Matrix    let m = 6;    let n = 5;      // Given Matrix    let 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 ]];      // Elements to be checked    let x = 42;    let y = 80;      // Function call    checkSameDiag(li, x, y, m, n);         // This code is contributed by splevel62.</script>
YES
 
Time Complexity: O(N * M) Auxiliary Space: O(1)
amit143katiyar
29AjayKumar
splevel62
romann
Kirti_Mangal
Matrix
Searching
Searching
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flood fill Algorithm - how to implement fill() in paint?
Breadth First Traversal ( BFS ) on a 2D array
Program to find the Sum of each Row and each Column of a Matrix
Python program to add two Matrices
Efficiently compute sums of diagonals of a matrix
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Find the Missing Number
K'th Smallest/Largest Element in Unsorted Array | Set 1