Center element of matrix equals sums of half diagonals

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 diagonals
bool 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 code
int 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum elements 
// that can be made equal with k updates
import 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 diagonals
def 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 code
if __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" )

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum 
// elements that can be made 
// equal with k updates
using 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 diagonals
function 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
?>

chevron_right


Output:

Yes

Time Complexity : O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Sam007, jit_t, chitranayal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.