Squares of Matrix Diagonal Elements
Last Updated :
12 Dec, 2022
You have given an integer matrix with odd dimensions. Find the square of the diagonal elements on both sides.
Examples:
Input : 1 2 3
4 5 6
7 8 9
Output : Diagonal one: 1 25 81
Diagonal two: 9 25 49
Input : 2 5 7
3 7 2
5 6 9
Output : Diagonal one : 4 49 81
Diagonal two : 49 49 25
Method 1: Firstly we find the diagonal element of the matrix and then we print the square of that element.
C++
#include <iostream>
using namespace std;
#define MAX 100
void diagonalsquare( int mat[][MAX], int row,
int column)
{
cout << "Diagonal one : " ;
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < column; j++)
if (i == j)
cout << mat[i][j] * mat[i][j] << " " ;
}
cout << " \nDiagonal two : " ;
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < column; j++)
if (i + j == column - 1)
cout << mat[i][j] * mat[i][j] << " " ;
}
}
int main()
{
int mat[][MAX] = { { 2, 5, 7 },
{ 3, 7, 2 },
{ 5, 6, 9 } };
diagonalsquare(mat, 3, 3);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 100 ;
static void diagonalsquare( int mat[][], int row,
int column)
{
System.out.print( "Diagonal one : " );
for ( int i = 0 ; i < row; i++)
{
for ( int j = 0 ; j < column; j++)
if (i == j)
System.out.print ( mat[i][j] * mat[i][j] + " " );
}
System.out.println();
System.out.print( "Diagonal two : " );
for ( int i = 0 ; i < row; i++)
{
for ( int j = 0 ; j < column; j++)
if (i + j == column - 1 )
System.out.print(mat[i][j] * mat[i][j] + " " );
}
}
public static void main (String[] args)
{
int mat[][] = { { 2 , 5 , 7 },
{ 3 , 7 , 2 },
{ 5 , 6 , 9 } };
diagonalsquare(mat, 3 , 3 );
}
}
|
Python3
def diagonalsquare(mat, row, column) :
print ( "Diagonal one : " , end = "")
for i in range ( 0 , row) :
for j in range ( 0 , column) :
if (i = = j) :
print ( "{} " . format (mat[i][j] *
mat[i][j]), end = "")
print ( " \nDiagonal two : " , end = "")
for i in range ( 0 , row) :
for j in range ( 0 , column) :
if (i + j = = column - 1 ) :
print ( "{} " . format (mat[i][j] *
mat[i][j]), end = "")
mat = [[ 2 , 5 , 7 ],
[ 3 , 7 , 2 ],
[ 5 , 6 , 9 ]]
diagonalsquare(mat, 3 , 3 )
|
C#
using System;
class GFG
{
static void diagonalsquare( int [,]mat, int row,
int column)
{
Console.Write( "Diagonal one : " );
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < column; j++)
if (i == j)
Console.Write ( mat[i,j] * mat[i,j] + " " );
}
Console.WriteLine();
Console.Write( "Diagonal two : " );
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < column; j++)
if (i + j == column - 1)
Console.Write(mat[i,j] * mat[i,j] + " " );
}
}
public static void Main ()
{
int [,]mat = {{ 2, 5, 7 },
{ 3, 7, 2 },
{ 5, 6, 9 }};
diagonalsquare(mat, 3, 3);
}
}
|
PHP
<?php
function diagonalsquare( $mat , $row ,
$column )
{
echo "Diagonal one : " ;
for ( $i = 0; $i < $row ; $i ++)
{
for ( $j = 0; $j < $column ; $j ++)
if ( $i == $j )
echo $mat [ $i ][ $j ] * $mat [ $i ][ $j ]
, " " ;
}
echo " \nDiagonal two : " ;
for ( $i = 0; $i < $row ; $i ++)
{
for ( $j = 0; $j < $column ; $j ++)
if ( $i + $j == $column - 1)
echo $mat [ $i ][ $j ] * $mat [ $i ][ $j ],
" " ;
}
}
$mat = array ( array ( 2, 5, 7 ),
array ( 3, 7, 2 ),
array ( 5, 6, 9 ) );
diagonalsquare( $mat , 3, 3);
?>
|
Javascript
let MAX = 100
function diagonalsquare(mat, row, column)
{
process.stdout.write( "Diagonal one : " );
for ( var i = 0; i < row; i++)
{
for ( var j = 0; j < column; j++)
if (i == j)
process.stdout.write(mat[i][j] * mat[i][j] + " " );
}
process.stdout.write( " \nDiagonal two : " );
for ( var i = 0; i < row; i++)
{
for ( var j = 0; j < column; j++)
if (i + j == column - 1)
process.stdout.write(mat[i][j] * mat[i][j] + " " );
}
}
let mat = [[ 2, 5, 7 ], [ 3, 7, 2 ], [ 5, 6, 9 ] ];
diagonalsquare(mat, 3, 3);
|
Output
Diagonal one : 4 49 81
Diagonal two : 49 49 25
Time Complexity : O(n*n)
Auxiliary Space: O(1)
Method 2:
An efficient solution is also the same as in the naive approach but in this, we are taking only one loop to find the diagonal element and then we print the square of that element.
C++
#include <iostream>
using namespace std;
#define MAX 100
void diagonalsquare( int mat[][MAX], int row,
int column)
{
cout << " \nDiagonal one : " ;
for ( int i = 0; i < row; i++)
{
cout << mat[i][i] * mat[i][i] << " " ;
}
cout << " \nDiagonal two : " ;
for ( int i = 0; i < row; i++)
{
cout << mat[i][row - i - 1] * mat[i][row - i - 1]
<< " " ;
}
}
int main()
{
int mat[][MAX] = { { 2, 5, 7 },
{ 3, 7, 2 },
{ 5, 6, 9 } };
diagonalsquare(mat, 3, 3);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAX = 100 ;
static void diagonalsquare( int mat[][], int row, int column)
{
System.out.print ( " Diagonal one : " );
for ( int i = 0 ; i < row; i++)
{
System.out.print( mat[i][i] * mat[i][i] + " " );
}
System.out.println();
System.out.print( " Diagonal two : " );
for ( int i = 0 ; i < row; i++)
{
System.out.print( mat[i][row - i - 1 ] *
mat[i][row - i - 1 ] + " " );
}
}
public static void main (String[] args)
{
int mat[][] = { { 2 , 5 , 7 },
{ 3 , 7 , 2 },
{ 5 , 6 , 9 } };
diagonalsquare(mat, 3 , 3 );
}
}
|
Python3
def diagonalsquare(mat, row,
column) :
print ( "Diagonal one : " ,
end = "")
for i in range ( 0 , row) :
print (mat[i][i] *
mat[i][i], end = " " )
print ( "\nDiagonal two : " ,
end = "")
for i in range ( 0 , row) :
print (mat[i][row - i - 1 ] *
mat[i][row - i - 1 ] ,
end = " " )
mat = [[ 2 , 5 , 7 ],
[ 3 , 7 , 2 ],
[ 5 , 6 , 9 ]]
diagonalsquare(mat, 3 , 3 )
|
C#
using System;
class GFG {
static int MAX =100;
static void diagonalsquare( int [,] mat,
int row,
int column)
{
Console.Write ( "Diagonal one : " );
for ( int i = 0; i < row; i++)
{
Console.Write(mat[i, i] *
mat[i, i] + " " );
}
Console.WriteLine();
Console.Write( "Diagonal two : " );
for ( int i = 0; i < row; i++)
{
Console.Write(mat[i, row - i - 1] *
mat[i, row - i - 1] + " " );
}
}
public static void Main ()
{
int [,] mat = new int [,]{{ 2, 5, 7 },
{ 3, 7, 2 },
{ 5, 6, 9 }};
diagonalsquare(mat, 3, 3);
}
}
|
PHP
<?php
$MAX = 100;
function diagonalsquare( $mat , $row ,
$column )
{
echo " \nDiagonal one : " ;
for ( $i = 0; $i < $row ; $i ++)
{
echo $mat [ $i ][ $i ] * $mat [ $i ][ $i ] , " " ;
}
echo " \nDiagonal two : " ;
for ( $i = 0; $i < $row ; $i ++)
{
echo $mat [ $i ][ $row - $i - 1] *
$mat [ $i ][ $row - $i - 1]
, " " ;
}
}
$mat = array ( array (2, 5, 7 ),
array (3, 7, 2 ),
array (5, 6, 9 ));
diagonalsquare( $mat , 3, 3);
?>
|
Javascript
let MAX = 100
function diagonalsquare(mat, row, column)
{
console.log( " Diagonal one : " );
for ( var i = 0; i < row; i++) {
process.stdout.write(mat[i][i] * mat[i][i] + " " );
}
process.stdout.write( " \nDiagonal two : " );
for ( var i = 0; i < row; i++) {
process.stdout.write(mat[i][row - i - 1]
* mat[i][row - i - 1]
+ " " );
}
}
let mat = [ [ 2, 5, 7 ], [ 3, 7, 2 ], [ 5, 6, 9 ] ];
diagonalsquare(mat, 3, 3);
|
Output
Diagonal one : 4 49 81
Diagonal two : 49 49 25
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...