Mirror Diagonals

Posted: 20 May, 2021
Difficulty: Easy


Try Problem

Ninja has a 2D array ‘arr’ with ‘N’ rows and each row contains ‘N’ elements. Your task is to check if the left diagonal numbers and right diagonal numbers are equal in the 2D Array.

Input Format:

The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains one integer ‘N’ denoting the number of rows and columns.

Each of the next ‘N’ lines contains ‘N’ integers separated by a single space.

Output Format:

For each test case, print ‘true’  if the diagonals numbers are equal, else print ‘false’.

The output of each test case will be printed in a separate line.


You do not need to input or print anything, as it has already been taken care of. Just implement the given function.


1 <= T <= 5
1 <= N <= 100
- 10 ^ 9 <= arr[i][j] <= 10 ^ 9

Where ‘T’ is the total number of test cases, ‘N’ is the number of rows and columns, arr[i][j] represents the elements of the 2D Array.
Approach 1

We only need to check whether the diagonal elements are equal or not. So we can only iterate over the main diagonal elements and do the checking.

For the left diagonal, 'i' should be equal to ‘j’ and for the right diagonal, sum of 'i' and ‘j’ must be ‘N’ - 1 where 'i' is the row index and 'j' is the column index. For the left diagonal, we can use ‘i’ in place of ‘j’ as they both are the same and for the right diagonal, using the relationship we can find ‘j’ that is equal to “N - 1 - i”

As we know the exact relationship between the indexes, we can only travel through those particular indexes and check the elements.




  • Run a loop “i”  =  0 to “N” for the diagonal elements
    • Compare arr[i][i] with arr[i][N - 1 - i]
      • If they are same,we can continue
      • Else return false.
  • Return true


Try Problem