Calculate sum of the main diagonal and the number of rows and columns containing repeated values in a square matrix
Given a matrix M[][] of dimensions N * N, consisting only of integers from the range 1 to N, the task is to compute the sum of the matrix elements present in the main diagonal, the number of rows and columns containing repeated values.
Examples:
Input: N = 4, M[][] = {{1, 2, 3, 4}, {2, 1, 4, 3}, {3, 4, 1, 2}, {4, 3, 2, 1}}
Output: 4 0 0
Explanation:
Sum of diagonal = M[0][0] + M[1][1] + M[2][2] + M[3][3] = 4.
No row or column consists of repeated elements.
Therefore, the required sum is 4
Input: N = 3, M[][] = {{2, 1, 3}, {1, 3, 2}, {1, 2, 3}}
Output: 8 0 2
Explanation:
Sum of diagonal = M[0][0]+M[1][1]+M[2][2] = 8.
No row consists of repeated elements.
1st and 3rd columns consists of repeated elements.
Approach 1: The approach is to simply traverse all elements of the matrix and find the sum of diagonals and the number of rows and columns having repeated values. Follow the steps below to solve the given problem:
- Initialize variables trace, rowRepeat, and columnRepeat to store the sum of the main diagonal, the number of rows, and columns containing repeated matrix elements respectively.
- Traverse through each element present in the matrix M[i][j] and increment the sum trace if i is equal to j.
- To find rows containing repeated values, iterate over one row at a time, compare values, and check if there exists a repeated value or not. On getting the first pair of repeat element, increment rowRepeat by 1 and break out of the loop.
- Repeat the above steps for every row of the matrix.
- The same procedure is repeated for all columns, where columnRepeat is incremented by 1 if the values match.
- After completing all the iterations, print the value of trace, rowRepeat, and columnRepeat as the result.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void vestigium( int N, int M[4][4])
{
int trace = 0, row_repeat = 0;
int column_repeat = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (i == j)
{
trace += M[i][j];
}
}
int flag1 = 0;
for ( int j = 0; j < N; j++)
{
for ( int k = 0; k < N; k++)
{
if (j != k && M[i][j] == M[i][k])
{
row_repeat++;
flag1 = 1;
break ;
}
}
if (flag1 == 1)
{
break ;
}
}
int flag2 = 0;
for ( int j = 0; j < N; j++)
{
for ( int k = 0; k < N; k++)
{
if (j != k && M[j][i] == M[k][i])
{
column_repeat++;
flag2 = 1;
break ;
}
}
if (flag2 == 1)
{
break ;
}
}
}
cout << trace << " "
<< row_repeat << " "
<< column_repeat ;
}
int main()
{
int M[4][4] = { { 1, 2, 3, 4 },
{ 2, 1, 4, 3 },
{ 3, 4, 1, 2 },
{ 4, 3, 2, 1 } };
int N = sizeof (M) / sizeof (M[0]);
vestigium(N, M);
}
|
Java
public class GFG {
public static String vestigium(
int N, int M[][])
{
int trace = 0 , row_repeat = 0 ;
int column_repeat = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (i == j) {
trace += M[i][j];
}
}
int flag1 = 0 ;
for ( int j = 0 ; j < N; j++) {
for ( int k = 0 ; k < N; k++) {
if (j != k
&& M[i][j] == M[i][k]) {
row_repeat++;
flag1 = 1 ;
break ;
}
}
if (flag1 == 1 ) {
break ;
}
}
int flag2 = 0 ;
for ( int j = 0 ; j < N; j++) {
for ( int k = 0 ; k < N; k++) {
if (j != k
&& M[j][i] == M[k][i]) {
column_repeat++;
flag2 = 1 ;
break ;
}
}
if (flag2 == 1 ) {
break ;
}
}
}
String output = trace + " "
+ row_repeat + " "
+ column_repeat + "\n" ;
return output;
}
public static void main(String[] args)
{
int M[][] = { { 1 , 2 , 3 , 4 },
{ 2 , 1 , 4 , 3 },
{ 3 , 4 , 1 , 2 },
{ 4 , 3 , 2 , 1 } };
int N = M.length;
String output = vestigium(N, M);
System.out.print(output);
}
}
|
Python3
def vestigium(N, M) :
trace = 0
row_repeat = 0
column_repeat = 0
for i in range (N) :
for j in range (N) :
if (i = = j):
trace + = M[i][j]
flag1 = 0
for j in range (N) :
for k in range (N) :
if (j ! = k and M[i][j] = = M[i][k]) :
row_repeat + = 1
flag1 = 1
break
if (flag1 = = 1 ) :
break
flag2 = 0
for j in range (N) :
for k in range (N) :
if (j ! = k and M[j][i] = = M[k][i]) :
column_repeat + = 1
flag2 = 1
break
if (flag2 = = 1 ) :
break
print (trace, row_repeat, column_repeat)
M = [[ 1 , 2 , 3 , 4 ],
[ 2 , 1 , 4 , 3 ],
[ 3 , 4 , 1 , 2 ],
[ 4 , 3 , 2 , 1 ]]
N = len (M)
vestigium(N, M)
|
C#
using System;
class GFG{
public static String vestigium( int N, int [,] M)
{
int trace = 0, row_repeat = 0;
int column_repeat = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (i == j)
{
trace += M[i, j];
}
}
int flag1 = 0;
for ( int j = 0; j < N; j++)
{
for ( int k = 0; k < N; k++)
{
if (j != k && M[i, j] == M[i, k])
{
row_repeat++;
flag1 = 1;
break ;
}
}
if (flag1 == 1)
{
break ;
}
}
int flag2 = 0;
for ( int j = 0; j < N; j++)
{
for ( int k = 0; k < N; k++)
{
if (j != k && M[j, i] == M[k, i])
{
column_repeat++;
flag2 = 1;
break ;
}
}
if (flag2 == 1)
{
break ;
}
}
}
string output = trace + " " + row_repeat + " " +
column_repeat + "\n" ;
return output;
}
public static void Main( string [] args)
{
int [, ] M = { { 1, 2, 3, 4 },
{ 2, 1, 4, 3 },
{ 3, 4, 1, 2 },
{ 4, 3, 2, 1 } };
int N = M.GetLength(0);
string output = vestigium(N, M);
Console.Write(output);
}
}
|
Javascript
<script>
function vestigium( N , M)
{
var trace = 0, row_repeat = 0;
var column_repeat = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (i == j) {
trace += M[i][j];
}
}
var flag1 = 0;
for (j = 0; j < N; j++) {
for (k = 0; k < N; k++) {
if (j != k
&& M[i][j] == M[i][k]) {
row_repeat++;
flag1 = 1;
break ;
}
}
if (flag1 == 1) {
break ;
}
}
var flag2 = 0;
for (j = 0; j < N; j++) {
for (k = 0; k < N; k++) {
if (j != k
&& M[j][i] == M[k][i]) {
column_repeat++;
flag2 = 1;
break ;
}
}
if (flag2 == 1) {
break ;
}
}
}
var output = trace + " "
+ row_repeat + " "
+ column_repeat + "\n" ;
return output;
}
var M = [ [ 1, 2, 3, 4 ],
[ 2, 1, 4, 3 ],
[ 3, 4, 1, 2 ],
[ 4, 3, 2, 1 ] ];
var N = M.length;
var output = vestigium(N, M);
document.write(output);
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(N2)
Approach 2:
- Create variables: trace, row_repeat, and column_repeat to zero.
- Initialize sets: row_set and column_set.
- Iterate through each row of the matrix using a loop.
- Clear row_set and column_set at the start of each row.
- Traverse elements in the current row and column.
- For diagonal elements (where i == j), add to trace.
- For each element in the row, check if it’s in row_set. If yes, increment row_repeat and break.
- For each element in the column, check if it’s in column_set. If yes, increment column_repeat and break.
- After all iterations, trace, row_repeat, and column_repeat are calculated.
- Print trace, row_repeat, and column_repeat.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void vestigium( int N, int M[4][4]) {
int trace = 0;
int row_repeat = 0;
int column_repeat = 0;
unordered_set< int > row_set, column_set;
for ( int i = 0; i < N; i++) {
trace += M[i][i];
row_set.clear();
column_set.clear();
for ( int j = 0; j < N; j++) {
if (row_set.count(M[i][j]) > 0) {
row_repeat++;
break ;
}
row_set.insert(M[i][j]);
if (column_set.count(M[j][i]) > 0) {
column_repeat++;
break ;
}
column_set.insert(M[j][i]);
}
}
cout << trace << " " << row_repeat << " " << column_repeat;
}
int main() {
int M[4][4] = {{1, 2, 3, 4},
{2, 1, 4, 3},
{3, 4, 1, 2},
{4, 3, 2, 1}};
int N = sizeof (M) / sizeof (M[0]);
vestigium(N, M);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int N = 4 ;
int [][] M = {
{ 1 , 2 , 3 , 4 },
{ 2 , 1 , 4 , 3 },
{ 3 , 4 , 1 , 2 },
{ 4 , 3 , 2 , 1 }
};
vestigium(N, M);
}
public static void vestigium( int N, int [][] M) {
int trace = 0 ;
int row_repeat = 0 ;
int column_repeat = 0 ;
Set<Integer> rowSet = new HashSet<>();
Set<Integer> columnSet = new HashSet<>();
for ( int i = 0 ; i < N; i++) {
trace += M[i][i];
rowSet.clear();
columnSet.clear();
for ( int j = 0 ; j < N; j++) {
if (rowSet.contains(M[i][j])) {
row_repeat++;
break ;
}
rowSet.add(M[i][j]);
if (columnSet.contains(M[j][i])) {
column_repeat++;
break ;
}
columnSet.add(M[j][i]);
}
}
System.out.println(trace + " " + row_repeat + " " + column_repeat);
}
}
|
Python3
def vestigium(N, M):
trace = 0
row_repeat = 0
column_repeat = 0
row_set = set ()
column_set = set ()
for i in range (N):
trace + = M[i][i]
row_set.clear()
column_set.clear()
for j in range (N):
if M[i][j] in row_set:
row_repeat + = 1
break
row_set.add(M[i][j])
if M[j][i] in column_set:
column_repeat + = 1
break
column_set.add(M[j][i])
print (trace, row_repeat, column_repeat)
if __name__ = = "__main__" :
M = [
[ 1 , 2 , 3 , 4 ],
[ 2 , 1 , 4 , 3 ],
[ 3 , 4 , 1 , 2 ],
[ 4 , 3 , 2 , 1 ]
]
N = len (M)
vestigium(N, M)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Vestigium( int N, int [,] M)
{
int trace = 0;
int rowRepeat = 0;
int columnRepeat = 0;
HashSet< int > rowSet = new HashSet< int >();
HashSet< int > columnSet = new HashSet< int >();
for ( int i = 0; i < N; i++)
{
trace += M[i, i];
rowSet.Clear();
columnSet.Clear();
for ( int j = 0; j < N; j++)
{
if (rowSet.Contains(M[i, j]))
{
rowRepeat++;
break ;
}
rowSet.Add(M[i, j]);
if (columnSet.Contains(M[j, i]))
{
columnRepeat++;
break ;
}
columnSet.Add(M[j, i]);
}
}
Console.WriteLine($ "{trace} {rowRepeat} {columnRepeat}" );
}
static void Main()
{
int [,] M = { {1, 2, 3, 4},
{2, 1, 4, 3},
{3, 4, 1, 2},
{4, 3, 2, 1} };
int N = M.GetLength(0);
Vestigium(N, M);
}
}
|
Javascript
function vestigium(N, M) {
let trace = 0;
let row_repeat = 0;
let column_repeat = 0;
let rowSet = new Set();
let columnSet = new Set();
for (let i = 0; i < N; i++) {
trace += M[i][i];
rowSet.clear();
columnSet.clear();
for (let j = 0; j < N; j++) {
if (rowSet.has(M[i][j])) {
row_repeat++;
break ;
}
rowSet.add(M[i][j]);
if (columnSet.has(M[j][i])) {
column_repeat++;
break ;
}
columnSet.add(M[j][i]);
}
}
console.log(trace + " " + row_repeat + " " + column_repeat);
}
const N = 4;
const M = [
[1, 2, 3, 4],
[2, 1, 4, 3],
[3, 4, 1, 2],
[4, 3, 2, 1]
];
vestigium(N, M);
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
09 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...