Program to convert the diagonal elements of the matrix to 0

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>
chevron_right

Output:
0 1 0 
3 0 2 
0 4 0
Another approach :
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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--;
        }         //otherwize the diagonal will be equivalaent 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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--;
            }
            //otherwize the diagonal will be equivalaent 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();
        }
          
    }
  
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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;
              
        # otherwize the diagonal will be equivalaent 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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--;
            }
            //otherwize the diagonal will be equivalaent 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
chevron_right

Output:
 0 1 0
 3 0 2
 0 4 0

Time complexity : O(n*m)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :