Find trace of matrix formed by adding Row-major and Column-major order of same matrix

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 contain 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 diagonal 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 matrixint 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 Programint 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 matriximport java.io.*;Ā
public 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 matrixdef 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 CodeN = 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 matrixusing 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 matrixfunction 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?> |
Javascript
<script>Ā
// Javascript 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 matrixfunction trace(n, m){Ā Ā Ā Ā Ā Ā Ā let A = new Array(n);Ā Ā Ā Ā Ā Ā Ā Ā Ā // Loop to create 2D array using 1D arrayĀ Ā Ā Ā for(var i = 0; i < A.length; i++) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā A[i] = new Array(2);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā let B = new Array(n);Ā Ā Ā Ā Ā Ā Ā Ā Ā // Loop to create 2D array using 1D arrayĀ Ā Ā Ā for(var i = 0; i < B.length; i++) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā B[i] = new Array(2);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā let C = new Array(n);Ā Ā Ā Ā Ā Ā Ā Ā Ā // Loop to create 2D array using 1D arrayĀ Ā Ā Ā for(var i = 0; i < C.length; i++)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā C[i] = new Array(2);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Generating the matrix AĀ Ā Ā Ā let cnt = 1;Ā Ā Ā Ā for(let i = 0; i < n; i++)Ā Ā Ā Ā Ā Ā Ā Ā for(let j = 0; j < m; j++) Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A[i][j] = cnt;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt++;Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā // Generating the matrix AĀ Ā Ā Ā cnt = 1;Ā Ā Ā Ā for(let i = 0; i < n; i++)Ā Ā Ā Ā Ā Ā Ā Ā for(let j = 0; j < m; j++) Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā B[j][i] = cnt;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt++;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding sum of matrix A and matrix BĀ Ā Ā Ā for(let i = 0; i < n; i++)Ā Ā Ā Ā Ā Ā Ā Ā for(let j = 0; j < m; j++)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā C[i][j] = A[i][j] + B[i][j]; Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding the trace of matrix C.Ā Ā Ā Ā let sum = 0;Ā Ā Ā Ā for(let i = 0; i < n; i++)Ā Ā Ā Ā Ā Ā Ā Ā for(let j = 0; j < m; j++)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i == j)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += C[i][j];Ā Ā Ā Ā Ā Ā Ā Ā Ā return sum;} Ā
// Driver codelet N = 3, M = 3;Ā Ā Ā document.write(trace(N, M));Ā
// This code is contributed by susmitakundugoaldangaĀ
</script> |
30
Time Complexity: O(N*M), as we are traversing the matrix using nested loops.
Auxiliary Space: O(N*M), as we are using extra space.
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 forms an Arithmetic Progression with a constant difference of a number of columns, 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 APint sn(int n, int an){Ā Ā Ā Ā return (n * (1 + an)) / 2;}Ā
// Return the trace of sum of row-major matrix// and column-major matrixint 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 Programint 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 matriximport 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 APdef sn(n, an):Ā Ā Ā Ā return (n * (1 + an)) / 2;Ā
# Return the trace of sum# of row-major matrix# and column-major matrixdef 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 CodeN = 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 matrixusing 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 APfunction sn($n, $an){Ā Ā Ā Ā return ($n * (1 + $an)) / 2;}Ā
// Return the trace of sum// of row-major matrix// and column-major matrixfunction 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?> |
Javascript
<script>Ā
// Javascript 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.Ā Ā Ā Ā Ā Ā Ā Ā let an = 1 + (n - 1) * (m + 1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Finding sum of first n integersĀ Ā Ā Ā Ā Ā Ā Ā // of AP in case of Row major matrixĀ Ā Ā Ā Ā Ā Ā Ā let 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Ā Ā Ā Ā Ā Ā Ā Ā let colmajorSum = sn(n, an);Ā
Ā Ā Ā Ā Ā Ā Ā Ā return rowmajorSum + colmajorSum;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driven ProgramĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let N = 3, M = 3;Ā Ā Ā Ā Ā Ā Ā Ā document.write(trace(N, M));Ā
// This code is contributed// by sravan kumar GottumukkalaĀ
</script> |
30
Ā Time Complexity: O(1), as we are not using any loops.
Auxiliary Space: O(1), as we are not using any extra space.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


