Center element of matrix equals sums of half diagonals - GeeksforGeeks

26 Apr, 2021
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.  
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  
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to check if the center// element is equal to the individual// sum of all the half diagonals#include <stdio.h>#include<bits/stdc++.h>using namespace std; const int MAX = 100; // Function to Check center element// is equal to the individual// sum of all the half diagonalsbool HalfDiagonalSums(int mat[][MAX], int n){       // Find sums of half diagonals    int diag1_left = 0, diag1_right = 0;    int diag2_left = 0, diag2_right = 0;       for (int i = 0, j = n - 1; i < n; i++, j--) {                 if (i < n/2) {            diag1_left += mat[i][i];            diag2_left += mat[j][i];                  }        else if (i > n/2) {            diag1_right += mat[i][i];            diag2_right += mat[j][i];                  }    }         return (diag1_left == diag2_right &&            diag2_right == diag2_left &&            diag1_right == diag2_left &&            diag2_right == mat[n/2][n/2]);} // Driver codeint main(){    int a[][MAX] = { { 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} };        cout << ( HalfDiagonalSums(a, 5) ? "Yes" : "No" );    return 0;}
// Java program to find maximum elements// that can be made equal with k updatesimport java.util.Arrays;public class GFG {         static int MAX = 100;         // Function to Check center element    // is equal to the individual    // sum of all the half diagonals    static boolean HalfDiagonalSums(int mat[][],                                          int n)    {                 // Find sums of half diagonals        int diag1_left = 0, diag1_right = 0;        int diag2_left = 0, diag2_right = 0;        for (int i = 0, j = n - 1; i < n;                                    i++, j--)        {                         if (i < n/2) {                diag1_left += mat[i][i];                diag2_left += mat[j][i];                    }            else if (i > n/2) {                diag1_right += mat[i][i];                diag2_right += mat[j][i];                    }        }                 return (diag1_left == diag2_right &&                diag2_right == diag2_left &&                diag1_right == diag2_left &&                diag2_right == mat[n/2][n/2]);    }         // Driver code    public static void main(String args[])    {                 int a[][] = { { 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} };                               System.out.print ( HalfDiagonalSums(a, 5)                                ? "Yes" : "No" );    }} // This code is contributed by Sam007
# Python 3 Program to check if the center# element is equal to the individual# sum of all the half diagonals  MAX = 100  # Function to Check center element# is equal to the individual# sum of all the half diagonalsdef HalfDiagonalSums( mat,  n):     # Find sums of half diagonals    diag1_left = 0    diag1_right = 0    diag2_left = 0    diag2_right = 0     i = 0    j = n - 1    while i < n:                  if (i < n//2) :            diag1_left += mat[i][i]            diag2_left += mat[j][i]                           elif (i > n//2) :            diag1_right += mat[i][i]            diag2_right += mat[j][i]                  i += 1        j -= 1          return (diag1_left == diag2_right and            diag2_right == diag2_left and            diag1_right == diag2_left and            diag2_right == mat[n//2][n//2])  # Driver codeif __name__ == "__main__":         a = [[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]]         print("Yes") if (HalfDiagonalSums(a, 5)) else print("No" )
// C# program to find maximum// elements that can be made// equal with k updatesusing System; class GFG{     // Function to Check    // center element is    // equal to the individual    // sum of all the half    // diagonals    static bool HalfDiagonalSums(int [,]mat,                                 int n)    {                 // Find sums of        // half diagonals        int diag1_left = 0,            diag1_right = 0;        int diag2_left = 0,            diag2_right = 0;        for (int i = 0, j = n - 1;                 i < n; i++, j--)        {                         if (i < n / 2)            {                diag1_left += mat[i, i];                diag2_left += mat[j, i];                }            else if (i > n / 2)            {                diag1_right += mat[i, i];                diag2_right += mat[j, i];                    }        }                 return (diag1_left == diag2_right &&                diag2_right == diag2_left &&                diag1_right == diag2_left &&                diag2_right == mat[n / 2, n / 2]);    }         // Driver code    static public void Main ()    {        int [,]a = {{ 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}};                             Console.WriteLine(HalfDiagonalSums(a, 5)?                                  "Yes" : "No" );    }} // This code is contributed by ajit
<?php// PHP Program to check if// the center element is// equal to the individual// sum of all the half diagonals$MAX = 100; // Function to Check center// element is equal to the// individual sum of all// the half diagonalsfunction HalfDiagonalSums($mat, $n){    global $MAX ;         // Find sums of    // half diagonals    $diag1_left = 1; $diag1_right = 1;    $diag2_left = 1; $diag2_right = 1;    for ($i = 0, $j = $n - 1;         $i < $n; $i++, $j--)    {                 if ($i < $n / 2)        {            $diag1_left += $mat[$i][$i];            $diag2_left += $mat[$j][$i];                }        else if ($i > $n / 2)        {            $diag1_right += $mat[$i][$i];            $diag2_right += $mat[$j][$i];            }    }         return ($diag1_left == $diag2_right &&            $diag2_right == $diag2_left &&            $diag1_right == $diag2_left &&            $diag2_right == $mat[$n / 2][$n / 2]);} // Driver code$a = array(array(2, 9, 1, 4, -2),           array(6, 7, 2, 11, 4),           array(4, 2, 9, 2, 4),           array(1, 9, 2, 4, 4),           array(0, 2, 4, 2, 5));if(HalfDiagonalSums($a, 5) == 0)    echo "Yes" ;else    echo "No" ;         // This code is contributed// by akt_mit?>
<script>// Javascript Program to check if the center// element is equal to the individual// sum of all the half diagonals const MAX = 100; // Function to Check center element// is equal to the individual// sum of all the half diagonalsfunction HalfDiagonalSums(mat, n){       // Find sums of half diagonals    let diag1_left = 0, diag1_right = 0;    let diag2_left = 0, diag2_right = 0;       for (let i = 0, j = n - 1; i < n; i++, j--) {                 if (i < parseInt(n/2)) {            diag1_left += mat[i][i];            diag2_left += mat[j][i];                  }        else if (i > parseInt(n/2)) {            diag1_right += mat[i][i];            diag2_right += mat[j][i];                  }    }         return (diag1_left == diag2_right &&            diag2_right == diag2_left &&            diag1_right == diag2_left &&            diag2_right == mat[parseInt(n/2)][parseInt(n/2)]);} // Driver code    let a = [ [ 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] ];        document.write( HalfDiagonalSums(a, 5) ? "Yes" : "No" ); // This code is contributed by subham348.</script>
Yes
 
Time Complexity : O(N)  
Sam007
jit_t
ukasp
subham348
Matrix
Misc
Technical Scripter
Misc
Misc
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inplace rotate square matrix by 90 degrees | Set 1
Count all possible paths from top left to bottom right of a mXn matrix
Min Cost Path | DP-6
Printing all solutions in N-Queen Problem
Efficiently compute sums of diagonals of a matrix
Top 10 algorithms in Interview Questions
vector::push_back() and vector::pop_back() in C++ STL
Overview of Data Structures | Set 1 (Linear Data Structures)
How to write Regular Expressions?
Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)