Maximum trace possible for any sub-matrix of the given matrix
Last Updated :
26 May, 2022
Given an N x N matrix mat[][], the task is to find the maximum trace possible for any sub-matrix of the given matrix.
Examples:
Input: mat[][] = {
{10, 2, 5},
{6, 10, 4},
{2, 7, -10}}
Output: 20
{{10, 2},
{6, 10}}
is the sub-matrix with the maximum trace.
Input: mat[][] = {
{1, 2, 5},
{6, 3, 4},
{2, 7, 1}}
Output: 13
Approach: An efficient approach is to take the sum along the diagonal from each element of the matrix and update the maximum sum as the trace of any square matrix is the sum of the elements on its main diagonal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 3
int MaxTraceSub( int mat[][N])
{
int max_trace = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
int r = i, s = j, trace = 0;
while (r < N && s < N) {
trace += mat[r][s];
r++;
s++;
max_trace = max(trace, max_trace);
}
}
}
return max_trace;
}
int main()
{
int mat[N][N] = { { 10, 2, 5 },
{ 6, 10, 4 },
{ 2, 7, -10 } };
cout << MaxTraceSub(mat);
return 0;
}
|
Java
class GFG
{
static int N = 3 ;
static int MaxTraceSub( int mat[][])
{
int max_trace = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
int r = i, s = j, trace = 0 ;
while (r < N && s < N)
{
trace += mat[r][s];
r++;
s++;
max_trace = Math.max(trace, max_trace);
}
}
}
return max_trace;
}
public static void main(String[] args)
{
int mat[][] = { { 10 , 2 , 5 },
{ 6 , 10 , 4 },
{ 2 , 7 , - 10 } };
System.out.println(MaxTraceSub(mat));
}
}
|
Python3
N = 3
def MaxTraceSub(mat):
max_trace = 0
for i in range (N):
for j in range (N):
r = i
s = j
trace = 0
while (r < N and s < N):
trace + = mat[r][s]
r + = 1
s + = 1
max_trace = max (trace, max_trace)
return max_trace
if __name__ = = '__main__' :
mat = [[ 10 , 2 , 5 ],[ 6 , 10 , 4 ],[ 2 , 7 , - 10 ]]
print (MaxTraceSub(mat))
|
C#
using System;
class GFG
{
static int N = 3;
static int MaxTraceSub( int [][]mat)
{
int max_trace = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
int r = i, s = j, trace = 0;
while (r < N && s < N)
{
trace += mat[r][s];
r++;
s++;
max_trace = Math.Max(trace, max_trace);
}
}
}
return max_trace;
}
public static void Main()
{
int [][] mat = new int [][]{ new int []{ 10, 2, 5 },
new int []{ 6, 10, 4 },
new int []{ 2, 7, -10 } };
Console.WriteLine(MaxTraceSub(mat));
}
}
|
PHP
<?php
$N = 3;
function MaxTraceSub( $mat )
{
global $N ;
$max_trace = 0;
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
{
$r = $i ;
$s = $j ;
$trace = 0;
while ( $r < $N && $s < $N )
{
$trace += $mat [ $r ][ $s ];
$r ++;
$s ++;
$max_trace = max( $trace ,
$max_trace );
}
}
}
return $max_trace ;
}
$mat = array ( array ( 10, 2, 5 ),
array ( 6, 10, 4 ),
array ( 2, 7, -10 ));
print (MaxTraceSub( $mat ));
?>
|
Javascript
<script>
var N = 3;
function MaxTraceSub(mat)
{
var max_trace = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
var r = i, s = j, trace = 0;
while (r < N && s < N) {
trace += mat[r][s];
r++;
s++;
max_trace = Math.max(trace, max_trace);
}
}
}
return max_trace;
}
var mat = [
[ 10, 2, 5 ],
[ 6, 10, 4 ],
[ 2, 7, -10 ] ];
document.write(MaxTraceSub(mat));
</script>
|
Time Complexity: O(N*N), as we are using nested loops for traversing the matrix.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...