Open In App

Center element of matrix equals sums of half diagonals

Last Updated : 09 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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;
}


Java




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


Python 3




# 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" )


C#




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


PHP




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


Javascript




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


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads