Related Articles

# Maximum trace possible for any sub-matrix of the given matrix

• Difficulty Level : Basic
• Last Updated : 05 May, 2021

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

 `// C++ implementation of the approach``#include ``using` `namespace` `std;``#define N 3` `// Function to return the maximum trace possible``// for a sub-matrix of the given matrix``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;` `            ``// Calculate the trace for each of``            ``// the sub-matrix with top left corner``            ``// at cell (r, s)``            ``while` `(r < N && s < N) {``                ``trace += mat[r][s];``                ``r++;``                ``s++;` `                ``// Update the maximum trace``                ``max_trace = max(trace, max_trace);``            ``}``        ``}``    ``}` `    ``// Return the maximum trace``    ``return` `max_trace;``}` `// Driver code``int` `main()``{``    ``int` `mat[N][N] = { { 10, 2, 5 },``                      ``{ 6, 10, 4 },``                      ``{ 2, 7, -10 } };``    ``cout << MaxTraceSub(mat);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG``{``    ` `static` `int` `N = ``3``;` `// Function to return the maximum trace possible``// for a sub-matrix of the given matrix``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``;` `            ``// Calculate the trace for each of``            ``// the sub-matrix with top left corner``            ``// at cell (r, s)``            ``while` `(r < N && s < N)``            ``{``                ``trace += mat[r][s];``                ``r++;``                ``s++;` `                ``// Update the maximum trace``                ``max_trace = Math.max(trace, max_trace);``            ``}``        ``}``    ``}` `    ``// Return the maximum trace``    ``return` `max_trace;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``        ``int` `mat[][] = { { ``10``, ``2``, ``5` `},``                    ``{ ``6``, ``10``, ``4` `},``                    ``{ ``2``, ``7``, -``10` `} };``    ``System.out.println(MaxTraceSub(mat));``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python 3 implementation of the approach``N ``=` `3` `# Function to return the maximum trace possible``# for a sub-matrix of the given matrix``def` `MaxTraceSub(mat):``    ``max_trace ``=` `0``    ``for` `i ``in` `range``(N):``        ``for` `j ``in` `range``(N):``            ``r ``=` `i``            ``s ``=` `j``            ``trace ``=` `0` `            ``# Calculate the trace for each of``            ``# the sub-matrix with top left corner``            ``# at cell (r, s)``            ``while` `(r < N ``and` `s < N):``                ``trace ``+``=` `mat[r]``                ``r ``+``=` `1``                ``s ``+``=` `1` `                ``# Update the maximum trace``                ``max_trace ``=` `max``(trace, max_trace)` `    ``# Return the maximum trace``    ``return` `max_trace` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``mat ``=` `[[``10``, ``2``, ``5``],[``6``, ``10``, ``4``],[``2``, ``7``, ``-``10``]]``    ``print``(MaxTraceSub(mat))` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG``{``    ` `static` `int` `N = 3;` `// Function to return the maximum trace possible``// for a sub-matrix of the given matrix``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;` `            ``// Calculate the trace for each of``            ``// the sub-matrix with top left corner``            ``// at cell (r, s)``            ``while` `(r < N && s < N)``            ``{``                ``trace += mat[r][s];``                ``r++;``                ``s++;` `                ``// Update the maximum trace``                ``max_trace = Math.Max(trace, max_trace);``            ``}``        ``}``    ``}` `    ``// Return the maximum trace``    ``return` `max_trace;``}` `// Driver code``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));``}``}` `// This code has been contributed``// by Akanksha Rai`

## PHP

 ``

## Javascript

 ``
Output:
`20`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up