Count rows/columns with sum equals to diagonal sum
Last Updated :
20 Feb, 2023
Given an n x n square matrix, count all rows and columns whose sum is equal to the sum of any principal diagonal or secondary diagonal. Examples:
Input : n = 3
arr[][] = { {1, 2, 3},
{4, 5, 2},
{7, 9, 10}};
Output : 2
In first example sum of principal diagonal
= (1 + 5 + 10) = 16 and sum of secondary
diagonal = (3 + 5 + 7) = 15.
Input: n = 4
arr[][] = { { 7, 2, 3, 5 },
{ 4, 5, 6, 3 },
{ 7, 9, 10, 12 },
{ 1, 5, 4, 3 } };
Output: 1
We need to count number of rows or columns whose sum is equal to 16 or 15. So find sum of all rows and columns and if their sum is equal to 16 or 15 then increment the count. Therefore, sum of column {2, 5, 9} = 16 and sum column {3, 2, 10} = 15. Hence, the count is equal to 2.
C++
#include <iostream>
#define n 4
using namespace std;
int count( int arr[][n])
{
int diag1 = 0, diag2 = 0;
int row = 0, col = 0, count = 0;
for ( int i = 0, j = n - 1; i < n; i++, j--)
{
diag1 += arr[i][i];
diag2 += arr[i][j];
}
for ( int i = 0; i < n; i++) {
row = 0, col = 0;
for ( int j = 0; j < n; j++) {
row = row + arr[i][j];
}
for ( int j = 0; j < n; j++) {
col = col + arr[j][i];
}
if ((row == diag1) || (row == diag2)) {
count++;
}
if ((col == diag1) || (col == diag2))
count++;
}
return count;
}
int main()
{
int arr[n][n] = { { 7, 2, 3, 5 },
{ 4, 5, 6, 3 },
{ 7, 9, 10, 12 },
{ 1, 5, 4, 3 } };
cout << count(arr) << endl;
}
|
Java
import java.io.*;
class GFG {
static int n = 4 ;
static int count( int arr[][])
{
int diag1 = 0 , diag2 = 0 ;
int row = 0 , col = 0 , count = 0 ;
for ( int i = 0 , j = n - 1 ; i < n; i++, j--)
{
diag1 += arr[i][i];
diag2 += arr[i][j];
}
for ( int i = 0 ; i < n; i++) {
row = 0 ;
col = 0 ;
for ( int j = 0 ; j < n; j++) {
row = row + arr[i][j];
}
for ( int j = 0 ; j < n; j++) {
col = col + arr[j][i];
}
if ((row == diag1) || (row == diag2))
count++;
if ((col == diag1) || (col == diag2))
count++;
}
return count;
}
public static void main(String[] args)
{
int arr[][] = {{ 7 , 2 , 3 , 5 },
{ 4 , 5 , 6 , 3 },
{ 7 , 9 , 10 , 12 },
{ 1 , 5 , 4 , 3 }};
System.out.println(count(arr));
}
}
|
Python3
n = 4
def count(arr):
diag1 = 0 ; diag2 = 0 ; row = 0
col = 0 ; count = 0 ; j = n - 1
for i in range (n):
diag1 + = arr[i][i]
diag2 + = arr[i][j]
j - = 1
for i in range (n):
row = 0 ; col = 0
for j in range (n):
row + = arr[i][j]
for j in range (n):
col + = arr[j][i]
if ((row = = diag1) or (row = = diag2)):
count + = 1
if ((col = = diag1) or (col = = diag2)):
count + = 1
return count
arr = [[ 7 , 2 , 3 , 5 ],
[ 4 , 5 , 6 , 3 ],
[ 7 , 9 , 10 , 12 ],
[ 1 , 5 , 4 , 3 ] ]
print (count(arr))
|
C#
using System;
namespace Matrix
{
public class GFG
{
static int n = 4;
static int count( int [,]arr)
{
int diag1 = 0, diag2 = 0;
int row = 0, col = 0, count = 0;
for ( int i = 0, j = n - 1; i < n; i++, j--)
{
diag1 += arr[i,i];
diag2 += arr[i,j];
}
for ( int i = 0; i < n; i++) {
row = 0;
col = 0;
for ( int j = 0; j < n; j++) {
row = row + arr[i,j];
}
for ( int j = 0; j < n; j++) {
col = col + arr[j,i];
}
if ((row == diag1) || (row == diag2))
count++;
if ((col == diag1) || (col == diag2))
count++;
}
return count;
}
public static void Main()
{
int [,]arr = { { 7, 2, 3, 5 },
{ 4, 5, 6, 3 },
{ 7, 9, 10, 12 },
{ 1, 5, 4, 3 } };
Console.Write(count(arr));
}
}
}
|
PHP
<?php
function countt( $arr )
{
$diag1 = 0; $diag2 = 0;
$row = 0; $col = 0;
$count = 0; $n = 4;
for ( $i = 0, $j = $n - 1; $i < $n ; $i ++, $j --)
{
$diag1 += $arr [ $i ][ $i ];
$diag2 += $arr [ $i ][ $j ];
}
for ( $i = 0; $i < $n ; $i ++) {
$row = 0; $col = 0;
for ( $j = 0; $j < $n ; $j ++) {
$row = $row + $arr [ $i ][ $j ];
}
for ( $j = 0; $j < $n ; $j ++) {
$col = $col + $arr [ $j ][ $i ];
}
if (( $row == $diag1 ) || ( $row == $diag2 )) {
$count ++;
}
if (( $col == $diag1 ) || ( $col == $diag2 ))
$count ++;
}
return $count ;
}
{
$arr = array ( array (7, 2, 3, 5),
array (4, 5, 6, 3),
array (7, 9, 10, 12),
array (1, 5, 4, 3));
echo countt( $arr ) ;
}
?>
|
Javascript
let n = 4
function count(arr)
{
let diag1 = 0, diag2 = 0;
let row = 0, col = 0, count = 0;
for (let i = 0, j = n - 1; i < n; i++, j--)
{
diag1 += arr[i][i];
diag2 += arr[i][j];
}
for (let i = 0; i < n; i++) {
row = 0, col = 0;
for (let j = 0; j < n; j++) {
row = row + arr[i][j];
}
for (let j = 0; j < n; j++) {
col = col + arr[j][i];
}
if ((row == diag1) || (row == diag2)) {
count++;
}
if ((col == diag1) || (col == diag2))
count++;
}
return count;
}
let arr = [[ 7, 2, 3, 5 ], [ 4, 5, 6, 3 ], [ 7, 9, 10, 12 ], [ 1, 5, 4, 3]];
console.log(count(arr))
|
Output:
1
Time complexity: O(n2) for given input n*n matrix
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...