Check if two elements of a matrix are on the same diagonal or not
Last Updated :
11 Jun, 2022
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: Yes
Explanation:
Both X and Y lie on the same diagonal.
Input: mat[][]= {{1, 2}. {3, 4}}, X = 2, Y = 4
Output: 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 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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG{
static void checkSameDiag( int li[][], 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)
{
System.out.println( "YES" );
}
else
System.out.println( "NO" );
}
public static void main(String[] args)
{
int m = 6 ;
int n = 5 ;
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 } };
int x = 42 ;
int y = 80 ;
checkSameDiag(li, x, y, m, n);
}
}
|
Python3
def checkSameDiag(x, y):
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
if P - Q = = I - J or P + Q = = I + J:
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
m, n = 6 , 5
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 ]]
x, y = 42 , 80
checkSameDiag(x, y)
|
C#
using System;
class GFG{
static void checkSameDiag( int [,]li, 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)
{
Console.WriteLine( "YES" );
}
else
Console.WriteLine( "NO" );
}
public static void Main(String[] args)
{
int m = 6;
int n = 5;
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}};
int x = 42;
int y = 80;
checkSameDiag(li, x, y, m, n);
}
}
|
Javascript
<script>
function checkSameDiag(li, x,
y, m, n)
{
let I = 0, J = 0;
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;
}
}
}
if (P - Q == I - J ||
P + Q == I + J)
{
document.write( "YES" );
}
else
document.write( "NO" );
}
let m = 6;
let n = 5;
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 ]];
let x = 42;
let y = 80;
checkSameDiag(li, x, y, m, n);
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...