VOOZH about

URL: https://www.geeksforgeeks.org/dsa/center-element-of-matrix-equals-sums-of-half-diagonals/

⇱ Center element of matrix equals sums of half diagonals - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Center element of matrix equals sums of half diagonals

Last Updated : 11 Jul, 2025

Given a matrix of odd order i.e(5*5). Task is to check if the center element of the matrix is equal to the individual sum of all the half diagonals. 
 

👁 Image

Examples:  

Input : mat[][] = { 2 9 1 4 -2
 6 7 2 11 4
 4 2 9 2 4
 1 9 2 4 4
 0 2 4 2 5 } 
Output : Yes
Explanation : 
 Sum of Half Diagonal 1 = 2 + 7 = 9
 Sum of Half Diagonal 2 = 9 + 0 = 9
 Sum of Half Diagonal 3 = 11 + -2 = 9
 Sum of Half Diagonal 4 = 5 + 4 = 9
Here, All the sums equal to the center element
that is mat[2][2] = 9

Simple Approach: 
Iterate two loops, find all half diagonal sums and then check all sums are equal to the center element of the matrix or not. If any one of them is not equal to center element Then print "No" Else "Yes". 
Time Complexity: O(N*N) 

Efficient Approach : is based on Efficient approach to find diagonal sum in O(N)

Below are the Implementation of this approach 


Output
Yes

Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.

Comment
Article Tags: