Program to check diagonal matrix and scalar matrix
Last Updated :
21 Dec, 2024
Given a square matrix mat[][] of order n*n, the task is to check if it is a Diagonal Matrix and Scalar matrix.
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, 0, 0, 0],
[0, 5, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 1]]
Output: True
Explanation: Main Diagonal = [4, 5, 2, 1], all the elements except main diagonal are zero.
Input: mat[][] = [[6, 10, 12, 0],
[0, 5, 0, 0],
[0, 0, 9, 0],
[0, 0, 0, 1]]
Output: False
Explanation: Main Diagonal = [6, 5, 9, 1], all the elements except main diagonal are not zero.
Approach:
The row and column indexes of Primary or Major diagonal are same i.e. lets say mat[][] is matrix then mat[i][i] will be a Major Diagonal element. The idea is to traverse the matrix and check if all elements except main diagonal are zero.
C++
// Program to check matrix is diagonal matrix or not.
#include <bits/stdc++.h>
using namespace std;
// Function to check matrix
// is diagonal matrix or not.
bool isDiagonalMatrix(vector<vector<int>> &mat) {
int n = mat.size();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) && (mat[i][j] != 0))
return false;
return true;
}
int main() {
vector<vector<int>> mat = { { 4, 0, 0, 0 },
{ 0, 7, 0, 0 },
{ 0, 0, 5, 0 },
{ 0, 0, 0, 1 } };
if (isDiagonalMatrix(mat))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
Java
// Program to check matrix is diagonal matrix or not.
import java.util.*;
class GfG {
// Function to check matrix
// is diagonal matrix or not.
static boolean isDiagonalMatrix(int[][] mat) {
int n = mat.length;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
// condition to check other elements
// except main diagonal are zero or not.
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("True");
else
System.out.println("False");
}
}
Python
# Python Program to check if matrix
# is diagonal matrix or not.
# Function to check matrix
# is diagonal matrix or not.
def isDiagonalMatrix(mat):
n = len(mat)
for i in range(0, n):
for j in range(0, n) :
# condition to check other elements
# except main diagonal are zero or not.
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("True")
else :
print("False")
C#
// C# Program to check if matrix
// is diagonal matrix or not.
using System;
class GfG {
// Function to check matrix
// is diagonal matrix or not.
static bool isDiagonalMatrix(int [,]mat) {
int n = mat.GetLength(0);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) && (mat[i,j] != 0))
return false;
return true;
}
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("True");
else
Console.WriteLine("False");
}
}
JavaScript
// JavaScript program to check matrix is
// diagonal matrix or not.
// Function to check matrix
// is diagonal matrix or not.
function isDiagonalMatrix(mat) {
let n = mat.length;
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) &&
(mat[i][j] != 0))
return false;
return true;
}
//driver code
let mat = [[4, 0, 0, 0],
[0, 7, 0, 0],
[0, 0, 5, 0],
[0, 0, 0, 1]];
if (isDiagonalMatrix(mat))
console.log("True");
else
console.log("False");
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.
Scalar Matrix: A square matrix is said to be a scalar matrix if all the main diagonal elements are equal and other elements 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, 0, 0, 0],
[0, 4, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 4]]
Output: True
Explanation: Main Diagonal = [4, 4, 4, 4] diagonal elements are equal and other elements are zero.
Input: mat[][] = [[4, 0, 0, 0],
[0, 4, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 4]]
Output: False
Explanation: Main Diagonal = [4, 4, 1, 4], diagonal elements are not equal.
Approach:
The row and column indexes of Primary or Major diagonal are same i.e. lets say mat[][] is matrix then mat[i][i] will be a Major Diagonal element. The idea is to traverse the matrix and check if all elements of main diagonal are equal and other elements are zero.
C++
// C++ Program to check matrix is scalar matrix or not.
#include <bits/stdc++.h>
using namespace std;
// Function to check matrix
// is scalar matrix or not.
bool isScalarMatrix(vector<vector<int>> &mat) {
int n = mat.size();
for (int i = 0; i < n; i++) {
// condition to check main diagonal
// elements are equal or not.
if((i > 0) && (mat[i][i] != mat[i-1][i-1]))
return false;
for (int j = 0; j < n; j++) {
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) && (mat[i][j] != 0))
return false;
}
}
return true;
}
int main() {
vector<vector<int>> mat = { { 4, 0, 0, 0 },
{ 0, 4, 0, 0 },
{ 0, 0, 4, 0 },
{ 0, 0, 0, 4 } };
if (isScalarMatrix(mat))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}
Java
// Java Program to check matrix is Scalar matrix or not.
import java.util.*;
class GfG {
// Function to check matrix
// is Scalar matrix or not.
static boolean isScalarMatrix(int[][] mat) {
int n = mat.length;
for (int i = 0; i < n; i++) {
// condition to check main diagonal
// elements are equal or not.
if((i > 0) && (mat[i][i] != mat[i-1][i-1]))
return false;
for (int j = 0; j < n; j++) {
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) && (mat[i][j] != 0))
return false;
}
}
return true;
}
public static void main(String[] args) {
int[][] mat = {
{ 4, 0, 0, 0 },
{ 0, 4, 0, 0 },
{ 0, 0, 4, 0 },
{ 0, 0, 0, 4 }
};
if (isScalarMatrix(mat))
System.out.println("True");
else
System.out.println("False");
}
}
Python
# Python Program to check if matrix
# is Scalar matrix or not.
def isScalarMatrix(mat):
n = len(mat)
for i in range(0, n):
# condition to check main diagonal
# elements are equal or not.
if (i > 0) and (mat[i][i] != mat[i - 1][i - 1]):
return False
for j in range(0, n) :
# condition to check other elements
# except main diagonal are zero or not.
if ((i != j) and (mat[i][j] != 0)) :
return False
return True
mat = [[ 4, 0, 0, 0 ],
[ 0, 4, 0, 0 ],
[ 0, 0, 4, 0 ],
[ 0, 0, 0, 4 ]]
if (isScalarMatrix(mat)) :
print("True")
else :
print("False")
C#
// C# Program to check matrix is
// Scalar matrix or not.
using System;
class GfG {
// Function to check matrix
// is Scalar matrix or not.
static bool isScalarMatrix(int [,]mat) {
int n = mat.GetLength(0);
for (int i = 0; i < n; i++) {
// condition to check main diagonal
// elements are equal or not.
if((i > 0) && (mat[i,i] != mat[i-1,i-1]))
return false;
for (int j = 0; j < n; j++) {
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) && (mat[i,j] != 0))
return false;
}
}
return true;
}
static void Main() {
int [,]mat = { { 4, 0, 0, 0 },
{ 0, 4, 0, 0 },
{ 0, 0, 4, 0 },
{ 0, 0, 0, 4 } };
if (isScalarMatrix(mat))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
JavaScript
// JavaScript program to check matrix is
// Scalar matrix or not.
// Function to check matrix
// is Scalar matrix or not.
function isScalarMatrix(mat) {
let n = mat.length;
for (let i = 0; i < n; i++){
// condition to check main diagonal
// elements are equal or not.
if((i > 0) && (mat[i][i] != mat[i-1][i-1]))
return false;
for (let j = 0; j < n; j++){
// condition to check other elements
// except main diagonal are zero or not.
if ((i != j) &&
(mat[i][j] != 0))
return false;
}
}
return true;
}
// driver code
let mat = [[4, 0, 0, 0],
[0, 4, 0, 0],
[0, 0, 4, 0],
[0, 0, 0, 4]];
if (isScalarMatrix(mat))
console.log("True");
else
console.log("False");
Time Complexity: O(n2), where n represents the number of rows and columns of the given matrix.
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem