Program to convert the diagonal elements of the matrix to 0
Last Updated :
15 Sep, 2022
Given an N*N matrix. The task is to convert the elements of diagonal of a matrix to 0.
Examples:
Input: mat[][] =
{{ 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 }}
Output:
{ {0, 1, 0},
{3, 0, 2},
{0, 4, 0}}
Input: mat[][] =
{{1, 3, 5, 6, 7},
{3, 5, 3, 2, 1},
{1, 2, 3, 4, 5},
{7, 9, 2, 1, 6},
{9, 1, 5, 3, 2}}
Output:
{{0, 3, 5, 6, 0},
{3, 0, 3, 0, 1},
{1, 2, 0, 4, 5},
{7, 0, 2, 0, 6},
{0, 1, 5, 3, 0}}
Approach: Run two loops i.e. outer loop for no. of rows and inner loop for no. of columns. Check for the below condition:
if ((i == j ) || (i + j + 1) == n)
mat[i][j] = 0
Below is the implementation of the above approach:
C++
// C++ program to change value of
// diagonal elements of a matrix to 0.
#include <iostream>
using namespace std;
const int MAX = 100;
// to print the resultant matrix
void print(int mat[][MAX], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
// function to change the values
// of diagonal elements to 0
void makediagonalzero(int mat[][MAX], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// right and left diagonal condition
if (i == j || (i + j + 1) == n)
mat[i][j] = 0;
}
}
// print resultant matrix
print(mat, n, m);
}
// Driver code
int main()
{
int n = 3, m = 3;
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
makediagonalzero(mat, n, m);
return 0;
}
Java
// Java program to change value of
// diagonal elements of a matrix to 0.
class GFG
{
static final int MAX = 100;
// to print the resultant matrix
static void print(int mat[][], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
// function to change the values
// of diagonal elements to 0
static void makediagonalzero(int mat[][],
int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// right and left diagonal condition
if (i == j || (i + j + 1) == n)
{
mat[i][j] = 0;
}
}
}
// print resultant matrix
print(mat, n, m);
}
// Driver code
public static void main(String args[])
{
int n = 3, m = 3;
int mat[][] = {{2, 1, 7},
{3, 7, 2},
{5, 4, 9}};
makediagonalzero(mat, n, m);
}
}
// This code is contributed
// by PrinciRaj1992
Python 3
# Python 3 program to change value of
# diagonal elements of a matrix to 0.
MAX = 100
# to print the resultant matrix
def print_1(mat, n, m):
for i in range(n):
for j in range(m):
print( mat[i][j], end = " ")
print()
# function to change the values
# of diagonal elements to 0
def makediagonalzero(mat, n, m):
for i in range(n):
for j in range(m):
# right and left diagonal condition
if (i == j or (i + j + 1) == n):
mat[i][j] = 0
# print resultant matrix
print_1(mat, n, m)
# Driver code
if __name__ == "__main__":
n = 3
m = 3
mat = [[ 2, 1, 7 ],
[ 3, 7, 2 ],
[ 5, 4, 9 ]]
makediagonalzero(mat, n, m)
# This code is contributed by ChitraNayal
C#
// C# program to change value of
// diagonal elements of a matrix to 0.
using System;
class GFG
{
static int MAX = 100;
// to print the resultant matrix
static void print(int[,] mat, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}
// function to change the values
// of diagonal elements to 0
static void makediagonalzero(int[,] mat,
int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// right and left diagonal condition
if (i == j || (i + j + 1) == n)
{
mat[i, j] = 0;
}
}
}
// print resultant matrix
print(mat, n, m);
}
// Driver code
public static void Main()
{
int n = 3, m = 3;
int[,] mat = {{2, 1, 7},
{3, 7, 2},
{5, 4, 9}};
makediagonalzero(mat, n, m);
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP program to change value of
// diagonal elements of a matrix to 0.
// to print the resultant matrix
function printsd(&$mat, $n, $m)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $m; $j++)
{
echo ($mat[$i][$j]);
echo (" ");
}
echo ("\n");
}
}
// function to change the values
// of diagonal elements to 0
function makediagonalzero(&$mat, $n, $m)
{
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $m; $j++)
{
// right and left diagonal condition
if ($i == $j || ($i + $j + 1) == $n)
$mat[$i][$j] = 0;
}
}
// print resultant matrix
printsd($mat, $n, $m);
}
// Driver code
$n = 3; $m = 3;
$mat = array(array(2, 1, 7),
array(3, 7, 2),
array(5, 4, 9));
makediagonalzero($mat, $n, $m);
// This code is contributed by Shivi_Aggarwal
?>
JavaScript
<script>
// Javascript program to change value of
// diagonal elements of a matrix to 0.
const MAX = 100;
// to print the resultant matrix
function print(mat, n, m)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++)
document.write(mat[i][j] + " ");
document.write("<br>");
}
}
// function to change the values
// of diagonal elements to 0
function makediagonalzero(mat, n, m)
{
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
// right and left diagonal condition
if (i == j || (i + j + 1) == n)
mat[i][j] = 0;
}
}
// print resultant matrix
print(mat, n, m);
}
// Driver code
let n = 3, m = 3;
let mat = [ [ 2, 1, 7 ],
[ 3, 7, 2 ],
[ 5, 4, 9 ] ];
makediagonalzero(mat, n, m);
// This code is contributed by souravmahato348.
</script>
C
// C program to change value of
// diagonal elements of a matrix to 0.
#include <stdio.h>
#define MAX 100
// to print the resultant matrix
void print(int mat[][MAX], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
printf("%d ",mat[i][j]);
printf("\n");
}
}
// function to change the values
// of diagonal elements to 0
void makediagonalzero(int mat[][MAX], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// right and left diagonal condition
if (i == j || (i + j + 1) == n)
mat[i][j] = 0;
}
}
// print resultant matrix
print(mat, n, m);
}
// Driver code
int main()
{
int n = 3, m = 3;
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
makediagonalzero(mat, n, m);
return 0;
}
Time Complexity: O(n*m)
Auxiliary Space: O(1)
Another Approach:
C++
// C++ program to change value of
// diagonal elements of a matrix to 0.
#include <bits/stdc++.h>
#define COL 100
using namespace std;
// Method to replace the diagonal matrix with zeros
void diagonalMat(int m[][COL], int row, int col)
{
// l is the left iterator which is
// iterationg from 0 to col-1[4] here
// k is the right iterator which is
// iterating from col-1 to 0
int i = 0, l = 0, k = col - 1;
// i used to iterate over rows of the matrix
while (i < row) {
int j = 0;
// condition to check if it is
// the centre of the matrix
if (l == k) {
m[l][k] = 0;
l++;
k--;
} // otherwise the diagonal will be equivalent to l
// or k increment l because l is traversing from
// left to right and decrement k for vice-cersa
else {
m[i][l] = 0;
l++;
m[i][k] = 0;
k--;
}
// print every element after replacing from the
// column
while (j < col) {
cout << " " << m[i][j];
j++;
}
i++;
cout << "\n";
}
}
// Driver code
int main()
{
int m[][COL]
= { { 2, 1, 7 }, { 3, 7, 2 }, { 5, 4, 9 } };
int row = 3, col = 3;
diagonalMat(m, row, col);
return 0;
}
// This code has been contributed by 29AjayKumar
C
// C program to change value of
// diagonal elements of a matrix to 0.
#include <stdio.h>
#define COL 100
// Method to replace the diagonal matrix with zeros
void diagonalMat( int m[][COL] ,int row, int col)
{
// l is the left iterator which is
// iterationg from 0 to col-1[4] here
//k is the right iterator which is
// iterating from col-1 to 0
int i = 0, l = 0, k = col - 1;
// i used to iterate over rows of the matrix
while (i < row)
{
int j = 0;
// condition to check if it is
// the centre of the matrix
if (l == k)
{
m[l][k] = 0;
l++;
k--;
} //otherwise the diagonal will be equivalent to l or k
//increment l because l is traversing from left
//to right and decrement k for vice-cersa
else
{
m[i][l] = 0;
l++;
m[i][k] = 0;
k--;
}
// print every element after replacing from the column
while (j < col)
{
printf(" %d",m[i][j]);
j++;
}
i++;
printf("\n");
}
}
// Driver code
int main()
{
int m[][COL] ={{ 2, 1, 7},
{ 3, 7, 2},
{ 5, 4, 9}};
int row = 3, col = 3;
diagonalMat( m, row, col);
return 0;
}
// This code has been contributed by kothavvsaakash
Java
// Java program to change value of
// diagonal elements of a matrix to 0.
import java.io.*;
class GFG {
public static void main (String[] args) {
int m[][] = {{ 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 }};
int row = 3, col = 3;
GFG.diagonalMat(row,col,m);
}
// method to replace the diagonal matrix with zeros
public static void diagonalMat(int row, int col, int m[][]){
// l is the left iterator which is
// iterationg from 0 to col-1[4] here
//k is the right iterator which is
// iterating from col-1 to 0
int i =0,l=0,k=col-1;
// i used to iterate over rows of the matrix
while(i<row){
int j=0;
// condition to check if it is
// the centre of the matrix
if(l==k){
m[l][k] = 0;
l++;
k--;
}
//otherwise the diagonal will be equivalent to l or k
//increment l because l is traversing from left
//to right and decrement k for vice-cersa
else{
m[i][l] = 0;
l++;
m[i][k]=0;
k--;
}
// print every element after replacing from the column
while(j<col){
System.out.print(" "+ m[i][j]);
j++;
}
i++;
System.out.println();
}
}
}
Python3
# Python3 program to change value of
# diagonal elements of a matrix to 0.
# method to replace the diagonal
# matrix with zeros
def diagonalMat(row, col, m):
# l is the left iterator which is
# iterationg from 0 to col-1[4] here
# k is the right iterator which is
# iterating from col-1 to 0
i, l, k = 0, 0, col - 1;
# i used to iterate over rows of the matrix
while(i < row):
j = 0;
# condition to check if it is
# the centre of the matrix
if(l == k):
m[l][k] = 0;
l += 1;
k -= 1;
# otherwise the diagonal will be equivalent to l or k
# increment l because l is traversing from left
# to right and decrement k for vice-cersa
else:
m[i][l] = 0;
l += 1;
m[i][k] = 0;
k -= 1;
# print every element
# after replacing from the column
while(j < col):
print(" ", m[i][j], end = "");
j += 1;
i += 1;
print("");
# Driver Code
if __name__ == '__main__':
m = [[2, 1, 7 ],
[ 3, 7, 2 ],
[ 5, 4, 9 ]];
row, col = 3, 3;
diagonalMat(row, col, m);
# This code contributed by Rajput-Ji
C#
// C# program to change value of
// diagonal elements of a matrix to 0.
using System;
class GFG {
public static void Main () {
int[,] m = {{ 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 }};
int row = 3, col = 3;
GFG.diagonalMat(row,col,m);
}
// method to replace the diagonal matrix with zeros
public static void diagonalMat(int row, int col, int[,] m){
// l is the left iterator which is
// iterationg from 0 to col-1[4] here
//k is the right iterator which is
// iterating from col-1 to 0
int i =0,l=0,k=col-1;
// i used to iterate over rows of the matrix
while(i < row){
int j=0;
// condition to check if it is
// the centre of the matrix
if(l==k){
m[l,k] = 0;
l++;
k--;
}
//otherwise the diagonal will be equivalent to l or k
//increment l because l is traversing from left
//to right and decrement k for vice-cersa
else{
m[i,l] = 0;
l++;
m[i,k]=0;
k--;
}
// print every element after replacing from the column
while(j < col){
Console.Write(" " + m[i,j]);
j++;
}
i++;
Console.WriteLine();
}
}
}
//This code is contributed by Mukul singh
JavaScript
<script>
// Javascript program to change value of
// diagonal elements of a matrix to 0.
const COL = 100;
// Method to replace the diagonal matrix with zeros
function diagonalMat(m, row, col)
{
// l is the left iterator which is
// iterationg from 0 to col-1[4] here
//k is the right iterator which is
// iterating from col-1 to 0
let i = 0, l = 0, k = col - 1;
// i used to iterate over rows of the matrix
while (i < row)
{
let j = 0;
// condition to check if it is
// the centre of the matrix
if (l == k)
{
m[l][k] = 0;
l++;
k--;
} //otherwise the diagonal will be equivalent to l or k
//increment l because l is traversing from left
//to right and decrement k for vice-cersa
else
{
m[i][l] = 0;
l++;
m[i][k] = 0;
k--;
}
// print every element after replacing from the column
while (j < col)
{
document.write(" " + m[i][j]);
j++;
}
i++;
document.write("<br>");
}
}
// Driver code
let m =[[ 2, 1, 7],
[ 3, 7, 2],
[ 5, 4, 9]];
let row = 3, col = 3;
diagonalMat( m, row, col);
</script>
Time Complexity: O(n*m)
Auxiliary Space: O(1)
Similar Reads
C++ Program to Efficiently Compute Sums of Diagonals of a Matrix
Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix. A00 A01 A02 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal
3 min read
C++ Program to Interchange Diagonals of Matrix
Given a square matrix of order n*n, you have to interchange the elements of both diagonals. Examples : Input : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : matrix[][] = {3, 2, 1, 4, 5, 6, 9, 8, 7} Input : matrix[][] = {4, 2, 3, 1, 5, 7, 6, 8, 9, 11, 10, 12, 16, 14, 15, 13} Output : matrix[][] =
2 min read
C++ program to Convert a Matrix to Sparse Matrix
Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: Input: Matrix: 0 1 1 1 2 2 2 1 3 3 2 5 4 3 4 Output: Sparse Matrix: 0 1 0 0 0 0 2 0 0 3 0 0 0 0 5 0 0 0 0 4 Explanation: Here the Sparse matrix is represented in the form Row Column Value Hence the ro
3 min read
C++ Program For Boundary Elements of a Matrix
Printing Boundary Elements of a Matrix. Given a matrix of size n x m. Print the boundary elements of the matrix. Boundary elements are those elements which are not surrounded by elements on all four directions, i.e. elements in first row, first column, last row and last column. Examples: Input: 1
4 min read
C++ Program to Rotate all Matrix elements except the diagonal K times by 90 degrees in clockwise direction
Given a square matrix mat[][] of dimension N and an integer K, the task is to rotate the matrix by 90 degrees K times without changing the position of the diagonal elements. Examples: Input: mat[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25
4 min read
C Program for Program to Interchange Diagonals of Matrix
Write a C program for a given square matrix of order n*n, the task is to interchange the elements of both diagonals. Examples : Input : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : matrix[][] = {3, 2, 1, 4, 5, 6, 9, 8, 7} Input : matrix[][] = {4, 2, 3, 1, 5, 7, 6, 8, 9, 11, 10, 12, 16, 14, 15,
2 min read
C++ Program to Generate a matrix having sum of secondary diagonal equal to a perfect square
Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square. Examples: Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32). Input:
3 min read
C++ Program to Rotate matrix by 45 degrees
Given a matrix mat[][] of size N*N, the task is to rotate the matrix by 45 degrees and print the matrix. Examples: Input: N = 6, mat[][] = {{3, 4, 5, 1, 5, 9, 5}, {6, 9, 8, 7, 2, 5, 2}, {1, 5, 9, 7, 5, 3, 2}, {4, 7, 8, 9, 3, 5, 2}, {4, 5, 2, 9, 5, 6, 2}, {4, 5, 7, 2, 9, 8, 3}}Output: 3 6 4 1 9 5 4 5
3 min read
C++ Program to Find difference between sums of two diagonals
Given a matrix of n X n. The task is to calculate the absolute difference between the sums of its diagonal.Examples: Input : mat[][] = 11 2 4 4 5 6 10 8 -12 Output : 15 Sum of primary diagonal = 11 + 5 + (-12) = 4. Sum of primary diagonal = 4 + 5 + 10 = 19. Difference = |19 - 4| = 15. Input : mat[][
3 min read
Find sum of all Boundary and Diagonal element of a Matrix
Given a 2D array arr[][] of order NxN, the task is to find the sum of all the elements present in both the diagonals and boundary elements of the given arr[][]. Examples: Input: arr[][] = { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} } Output: 40 Explanation: The Sum of elements on the bo
6 min read