Given two integers N and M. Consider two matrix ANXM, BNXM. Both matrix A and matrix B contains elements from 1 to N*M. Matrix A contains elements in Row-major order and matrix B contains elements in Column-major order. The task is to find the trace of the matrix formed by addition of A and B. Trace of matrix PNXM is defined as P[0][0] + P[1][1] + P[2][2] +….. + P[min(n – 1, m – 1)][min(n – 1, m – 1)] i.e addition of main diagonal.
Note – Both matrix A and matrix B contains elements from 1 to N*M.
Examples :
Input : N = 3, M = 3 Output : 30 Therefore, 1 2 3 A = 4 5 6 7 8 9 1 4 7 B = 2 5 8 3 6 9 2 6 10 A + B = 6 10 14 10 14 18 Trace = 2 + 10 + 18 = 30
Method 1 (Naive Approach) :
Generate matrix A and B and find the sum. Then traverse the main diagnol and find the sum.
Below is the implementation of this approach:
C++
// C++ program to find // trace of matrix formed by // adding Row-major and // Column-major order of same matrix #include <bits/stdc++.h> using namespace std; // Return the trace of // sum of row-major matrix // and column-major matrix int trace( int n, int m) { int A[n][m], B[n][m], C[n][m]; // Generating the matrix A int cnt = 1; for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) { A[i][j] = cnt; cnt++; } // Generating the matrix A cnt = 1; for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) { B[j][i] = cnt; cnt++; } // Finding sum of matrix A and matrix B for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) C[i][j] = A[i][j] + B[i][j]; // Finding the trace of matrix C. int sum = 0; for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) if (i == j) sum += C[i][j]; return sum; } // Driven Program int main() { int N = 3, M = 3; cout << trace(N, M) << endl; return 0; } |
Java
// Java program to find // trace of matrix formed by // adding Row-major and // Column-major order of same matrix class GFG { // Return the trace of // sum of row-major matrix // and column-major matrix static int trace( int n, int m) { int A[][] = new int [n][m]; int B[][] = new int [n][m]; int C[][] = new int [n][m]; // Generating the matrix A int cnt = 1 ; for ( int i = 0 ; i < n; i++) for ( int j = 0 ; j < m; j++) { A[i][j] = cnt; cnt++; } // Generating the matrix A cnt = 1 ; for ( int i = 0 ; i < n; i++) for ( int j = 0 ; j < m; j++) { B[j][i] = cnt; cnt++; } // Finding sum of matrix A and matrix B for ( int i = 0 ; i < n; i++) for ( int j = 0 ; j < m; j++) C[i][j] = A[i][j] + B[i][j]; // Finding the trace of matrix C. int sum = 0 ; for ( int i = 0 ; i < n; i++) for ( int j = 0 ; j < m; j++) if (i == j) sum += C[i][j]; return sum; } // Driver code public static void main (String[] args) { int N = 3 , M = 3 ; System.out.println(trace(N, M)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to find trace of matrix # formed by adding Row-major and # Column-major order of same matrix # Return the trace of sum of row-major # matrix and column-major matrix def trace(n, m): A = [[ 0 for x in range (m)] for y in range (n)]; B = [[ 0 for x in range (m)] for y in range (n)]; C = [[ 0 for x in range (m)] for y in range (n)]; # Generating the matrix A cnt = 1 ; for i in range (n): for j in range (m): A[i][j] = cnt; cnt + = 1 ; # Generating the matrix A cnt = 1 ; for i in range (n): for j in range (m): B[j][i] = cnt; cnt + = 1 ; # Finding sum of matrix A and matrix B for i in range (n): for j in range (m): C[i][j] = A[i][j] + B[i][j]; # Finding the trace of matrix C. sum = 0 ; for i in range (n): for j in range (m): if (i = = j): sum + = C[i][j]; return sum ; # Driver Code N = 3 ; M = 3 ; print (trace(N, M)); # This code is contributed by mits |
C#
// C# program to find // trace of matrix formed by // adding Row-major and // Column-major order of same matrix using System; class GFG { // Return the trace of // sum of row-major matrix // and column-major matrix static int trace( int n, int m) { int [, ] A = new int [n, m]; int [, ] B = new int [n, m]; int [, ] C = new int [n, m]; // Generating the matrix A int cnt = 1; for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) { A[i, j] = cnt; cnt++; } // Generating the matrix A cnt = 1; for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) { B[j, i] = cnt; cnt++; } // Finding sum of matrix A and matrix B for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) C[i, j] = A[i, j] + B[i, j]; // Finding the trace of matrix C. int sum = 0; for ( int i = 0; i < n; i++) for ( int j = 0; j < m; j++) if (i == j) sum += C[i, j]; return sum; } // Driver code public static void Main() { int N = 3, M = 3; Console.WriteLine(trace(N, M)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find trace of matrix // formed by adding Row-major and // Column-major order of same matrix // Return the trace of sum of row-major // matrix and column-major matrix function trace( $n , $m ) { $A = array_fill (0, $n , array_fill (0, $m , 0)); $B = array_fill (0, $n , array_fill (0, $m , 0)); $C = array_fill (0, $n , array_fill (0, $m , 0)); // Generating the matrix A $cnt = 1; for ( $i = 0; $i < $n ; $i ++) for ( $j = 0; $j < $m ; $j ++) { $A [ $i ][ $j ] = $cnt ; $cnt ++; } // Generating the matrix A $cnt = 1; for ( $i = 0; $i < $n ; $i ++) for ( $j = 0; $j < $m ; $j ++) { $B [ $j ][ $i ] = $cnt ; $cnt ++; } // Finding sum of matrix A and matrix B for ( $i = 0; $i < $n ; $i ++) for ( $j = 0; $j < $m ; $j ++) $C [ $i ][ $j ] = $A [ $i ][ $j ] + $B [ $i ][ $j ]; // Finding the trace of matrix C. $sum = 0; for ( $i = 0; $i < $n ; $i ++) for ( $j = 0; $j < $m ; $j ++) if ( $i == $j ) $sum += $C [ $i ][ $j ]; return $sum ; } // Driver Code $N = 3; $M = 3; print (trace( $N , $M )); // This code is contributed by mits ?> |
Output :
30
Time Complexity: O(N*M).
Method 2 (efficient approach) :
Basically, we need to find the sum of main diagonal of the first matrix A and main diagonal of the second matrix B.
Let’s take an example, N = 3, M = 4.
Therefore, Row-major matrix will be,
1 2 3 4 A = 5 6 7 8 9 10 11 12
So, we need the sum of 1, 6, 11.
Observe, it form an Arithmetic Progression with constant difference of number of column, M.
Also, first element is always 1. So, AP formed in case of Row-major matrix is 1, 1+(M+1), 1+2*(M+1), ….. consisting of N (number of rows) elements. And we know,
Sn = (n * (a1 + an))/2
So, n = R, a1 = 1, an = 1 + (R – 1)*(M+1).
Similarly, in case of Column-major, AP formed will be 1, 1+(N+1), 1+2*(N+1), …..
So, n = R, a1 = 1, an = 1 + (R – 1)*(N+1).
Below is the implementation of this approach:
C++
// C++ program to find trace of matrix formed // by adding Row-major and Column-major order // of same matrix #include <bits/stdc++.h> using namespace std; // Return sum of first n integers of an AP int sn( int n, int an) { return (n * (1 + an)) / 2; } // Return the trace of sum of row-major matrix // and column-major matrix int trace( int n, int m) { // Finding nth element in // AP in case of Row major matrix. int an = 1 + (n - 1) * (m + 1); // Finding sum of first n integers // of AP in case of Row major matrix int rowmajorSum = sn(n, an); // Finding nth element in AP // in case of Row major matrix an = 1 + (n - 1) * (n + 1); // Finding sum of first n integers // of AP in case of Column major matrix int colmajorSum = sn(n, an); return rowmajorSum + colmajorSum; } // Driven Program int main() { int N = 3, M = 3; cout << trace(N, M) << endl; return 0; } |
Java
// Java program to find trace of matrix formed // by adding Row-major and Column-major order // of same matrix import java.io.*; public class GFG { // Return sum of first n integers of an AP static int sn( int n, int an) { return (n * (1 + an)) / 2; } // Return the trace of sum of row-major matrix // and column-major matrix static int trace( int n, int m) { // Finding nth element in // AP in case of Row major matrix. int an = 1 + (n - 1) * (m + 1); // Finding sum of first n integers // of AP in case of Row major matrix int rowmajorSum = sn(n, an); // Finding nth element in AP // in case of Row major matrix an = 1 + (n - 1) * (n + 1); // Finding sum of first n integers // of AP in case of Column major matrix int colmajorSum = sn(n, an); return rowmajorSum + colmajorSum; } // Driven Program static public void main(String[] args) { int N = 3, M = 3; System. out .println(trace(N, M)); } } // This code is contributed by vt_m. |
Python3
# Python3 program to find trace # of matrix formed by adding # Row-major and Column-major # order of same matrix # Return sum of first n # integers of an AP def sn(n, an): return (n * ( 1 + an)) / 2 ; # Return the trace of sum # of row-major matrix # and column-major matrix def trace(n, m): # Finding nth element # in AP in case of # Row major matrix. an = 1 + (n - 1 ) * (m + 1 ); # Finding sum of first # n integers of AP in # case of Row major matrix rowmajorSum = sn(n, an); # Finding nth element in AP # in case of Row major matrix an = 1 + (n - 1 ) * (n + 1 ); # Finding sum of first n # integers of AP in case # of Column major matrix colmajorSum = sn(n, an); return int (rowmajorSum + colmajorSum); # Driver Code N = 3 ; M = 3 ; print (trace(N, M)); # This code is contributed mits |
C#
// C# program to find trace of matrix formed // by adding Row-major and Column-major order // of same matrix using System; public class GFG { // Return sum of first n integers of an AP static int sn( int n, int an) { return (n * (1 + an)) / 2; } // Return the trace of sum of row-major matrix // and column-major matrix static int trace( int n, int m) { // Finding nth element in // AP in case of Row major matrix. int an = 1 + (n - 1) * (m + 1); // Finding sum of first n integers // of AP in case of Row major matrix int rowmajorSum = sn(n, an); // Finding nth element in AP // in case of Row major matrix an = 1 + (n - 1) * (n + 1); // Finding sum of first n integers // of AP in case of Column major matrix int colmajorSum = sn(n, an); return rowmajorSum + colmajorSum; } // Driven Program static public void Main() { int N = 3, M = 3; Console.WriteLine(trace(N, M)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find trace of matrix formed // by adding Row-major and Column-major order // of same matrix // Return sum of first n integers of an AP function sn( $n , $an ) { return ( $n * (1 + $an )) / 2; } // Return the trace of sum // of row-major matrix // and column-major matrix function trace( $n , $m ) { // Finding nth element in // AP in case of Row major matrix. $an = 1 + ( $n - 1) * ( $m + 1); // Finding sum of first n integers // of AP in case of Row major matrix $rowmajorSum = sn( $n , $an ); // Finding nth element in AP // in case of Row major matrix $an = 1 + ( $n - 1) * ( $n + 1); // Finding sum of first n integers // of AP in case of Column major matrix $colmajorSum = sn( $n , $an ); return $rowmajorSum + $colmajorSum ; } // Driver Code $N = 3; $M = 3; echo trace( $N , $M ), "\n" ; // This code is contributed ajit ?> |
Output :
30
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.