Diagonal matrix
A square matrix is said to be a diagonal matrix if the elements of the matrix except the main diagonal are zero. A square null matrix is also a diagonal matrix whose main diagonal elements are zero.
Examples:
Input:
Mat[4][4] = {{4, 0, 0, 0},
{0, 5, 0, 0},
{0, 0, 2, 0},
{0, 0, 0, 1}}
Output: Yes
Input:
Mat[4][4] = {{6, 10, 12, 0},
{0, 5, 0, 0},
{0, 0, 9, 0},
{0, 0, 0, 1}}
Output: No
Below is the implementation:
CPP
#include <bits/stdc++.h>
#define N 4
using namespace std;
bool isDiagonalMatrix( int mat[N][N])
{
for ( int i = 0; i < N; i++)
for ( int j = 0; j < N; j++)
if ((i != j) && (mat[i][j] != 0))
return false ;
return true ;
}
int main()
{
int mat[N][N] = { { 4, 0, 0, 0 },
{ 0, 7, 0, 0 },
{ 0, 0, 5, 0 },
{ 0, 0, 0, 1 } };
if (isDiagonalMatrix(mat))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int N = 4 ;
static boolean isDiagonalMatrix( int mat[][])
{
for ( int i = 0 ; i < N; i++)
for ( int j = 0 ; j < N; j++)
if ((i != j) &&
(mat[i][j] != 0 ))
return false ;
return true ;
}
public static void main(String args[])
{
int mat[][] = { { 4 , 0 , 0 , 0 },
{ 0 , 7 , 0 , 0 },
{ 0 , 0 , 5 , 0 },
{ 0 , 0 , 0 , 1 } };
if (isDiagonalMatrix(mat))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
N = 4
def isDiagonalMatrix(mat) :
for i in range ( 0 , N):
for j in range ( 0 , N) :
if ((i ! = j) and
(mat[i][j] ! = 0 )) :
return False
return True
mat = [[ 4 , 0 , 0 , 0 ],
[ 0 , 7 , 0 , 0 ],
[ 0 , 0 , 5 , 0 ],
[ 0 , 0 , 0 , 1 ]]
if (isDiagonalMatrix(mat)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static int N = 4;
static bool isDiagonalMatrix( int [,]mat)
{
for ( int i = 0; i < N; i++)
for ( int j = 0; j < N; j++)
if ((i != j) && (mat[i,j] != 0))
return false ;
return true ;
}
public static void Main()
{
int [,]mat = { { 4, 0, 0, 0 },
{ 0, 7, 0, 0 },
{ 0, 0, 5, 0 },
{ 0, 0, 0, 1 } };
if (isDiagonalMatrix(mat))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
$N = 4;
function isDiagonalMatrix( $mat )
{
global $N ;
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
if (( $i != $j ) &&
( $mat [ $i ][ $j ] != 0))
return false;
return true;
}
$mat = array ( array (4, 0, 0, 0),
array (0, 7, 0, 0),
array (0, 0, 5, 0),
array (0, 0, 0, 1));
if (isDiagonalMatrix( $mat ))
echo "Yes" , "\n" ;
else
echo "No" , "\n" ;
?>
|
Javascript
<script>
let N = 4;
function isDiagonalMatrix(mat)
{
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++)
if ((i != j) &&
(mat[i][j] != 0))
return false ;
return true ;
}
let mat = [[ 4, 0, 0, 0 ],
[ 0, 7, 0, 0 ],
[ 0, 0, 5, 0 ],
[ 0, 0, 0, 1 ]];
if (isDiagonalMatrix(mat))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N2), where N represents the number of rows and columns of the given matrix.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Scalar matrix
A square matrix is said to be a scalar matrix if all the main diagonal elements are equal and other elements except main diagonal are zero. The scalar matrix can also be written in form of n * I, where n is any real number and I is the identity matrix.
Examples:
Input:
Mat[4][4] = {{4, 0, 0, 0},
{0, 4, 0, 0},
{0, 0, 4, 0},
{0, 0, 0, 4}}
Output: Yes
Input:
Mat[4][4] = {{4, 0, 0, 0},
{0, 4, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 4}}
Output: No
Below is the implementation:
CPP
#include <bits/stdc++.h>
#define N 4
using namespace std;
bool isScalarMatrix( int mat[N][N])
{
for ( int i = 0; i < N; i++)
for ( int j = 0; j < N; j++)
if ((i != j) && (mat[i][j] != 0))
return false ;
for ( int i = 0; i < N - 1; i++)
if (mat[i][i] != mat[i + 1][i + 1])
return false ;
return true ;
}
int main()
{
int mat[N][N] = { { 2, 0, 0, 0 },
{ 0, 2, 0, 0 },
{ 0, 0, 2, 0 },
{ 0, 0, 0, 2 } };
if (isScalarMatrix(mat))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int N = 4 ;
static boolean isScalarMatrix( int mat[][])
{
for ( int i = 0 ; i < N; i++)
for ( int j = 0 ; j < N; j++)
if ((i != j)
&& (mat[i][j] != 0 ))
return false ;
for ( int i = 0 ; i < N - 1 ; i++)
if (mat[i][i] != mat[i + 1 ][i + 1 ])
return false ;
return true ;
}
public static void main(String args[])
{
int mat[ ][ ] = { { 2 , 0 , 0 , 0 },
{ 0 , 2 , 0 , 0 },
{ 0 , 0 , 2 , 0 },
{ 0 , 0 , 0 , 2 } };
if (isScalarMatrix(mat))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
N = 4
def isScalarMatrix(mat) :
for i in range ( 0 ,N) :
for j in range ( 0 ,N) :
if ((i ! = j)
and (mat[i][j] ! = 0 )) :
return False
for i in range ( 0 ,N - 1 ) :
if (mat[i][i] ! =
mat[i + 1 ][i + 1 ]) :
return False
return True
mat = [[ 2 , 0 , 0 , 0 ],
[ 0 , 2 , 0 , 0 ],
[ 0 , 0 , 2 , 0 ],
[ 0 , 0 , 0 , 2 ]]
if (isScalarMatrix(mat)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static int N = 4;
static bool isScalarMatrix( int [,]mat)
{
for ( int i = 0; i < N; i++)
for ( int j = 0; j < N; j++)
if ((i != j) && (mat[i,j] != 0))
return false ;
for ( int i = 0; i < N - 1; i++)
if (mat[i, i] != mat[i + 1, i + 1])
return false ;
return true ;
}
public static void Main()
{
int [,]mat = { { 2, 0, 0, 0 },
{ 0, 2, 0, 0 },
{ 0, 0, 2, 0 },
{ 0, 0, 0, 2 } };
if (isScalarMatrix(mat))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
$N = 4;
function isScalarMatrix( $mat )
{
global $N ;
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
if (( $i != $j ) &&
( $mat [ $i ][ $j ] != 0))
return false;
for ( $i = 0; $i < $N - 1; $i ++)
if ( $mat [ $i ][ $i ] != $mat [ $i + 1][ $i + 1])
return false;
return true;
}
$mat = array ( array (2, 0, 0, 0),
array (0, 2, 0, 0),
array (0, 0, 2, 0),
array (0, 0, 0, 2));
if (isScalarMatrix( $mat ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
let N = 4;
function isScalarMatrix(mat)
{
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++)
if ((i != j)
&& (mat[i][j] != 0))
return false ;
for (let i = 0; i < N - 1; i++)
if (mat[i][i] != mat[i + 1][i + 1])
return false ;
return true ;
}
let mat = [[ 2, 0, 0, 0 ],
[ 0, 2, 0, 0 ],
[ 0, 0, 2, 0 ],
[ 0, 0, 0, 2 ]];
if (isScalarMatrix(mat))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N2), where N represents the number of rows and columns of the given matrix.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Aug, 2022
Like Article
Save Article