Tuesday, September 24, 2024
Google search engine
HomeData Modelling & AIPrint the matrix diagonally downwards

Print the matrix diagonally downwards

Given a matrix of size n*n, print the matrix in the following pattern.

Output: 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16

Examples: 

Input :matrix[2][2]= { {1, 2},
                       {3, 4} }
Output : 1 2 3 4

Input :matrix[3][3]= { {1, 2, 3},
                       {4, 5, 6},
                       {7, 8, 9} }
Output : 1 2 4 3 5 7 6 8 9

Implementation: Following is the C++ implementation for the above pattern. 

C++




// CPP program to print matrix downward
#include <bits/stdc++.h>
using namespace std;
 
void printMatrixDiagonallyDown(vector<vector<int> > matrix,
                                                    int n)
{
    // printing elements above and on
    // second diagonal
    for (int k = 0; k < n; k++) {
 
        // traversing downwards starting
        // from first row
        int row = 0, col = k;
        while (col >= 0) {
            cout << matrix[row][col] << " ";
            row++, col--;
        }
    }
 
    // printing elements below second
    // diagonal
    for (int j = 1; j < n; j++) {
 
        // traversing downwards starting
        // from last column
        int col = n - 1, row = j;
        while (row < n) {
            cout << matrix[row][col] << " ";
            row++, col--;
        }
    }
}
 
int main()
{
    vector<vector<int> > matrix{ { 1, 2, 3 },
                                 { 4, 5, 6 },
                                 { 7, 8, 9 } };
    int n = 3;
    printMatrixDiagonallyDown(matrix, n);
    return 0;
}


Java




// JAVA program to print
// matrix downward
class GFG{
static void printMatrixDiagonallyDown(int[][] matrix,
                                      int n)
{
  // printing elements above and on
  // second diagonal
  for (int k = 0; k < n; k++)
  {
    // traversing downwards
    // starting from first row
    int row = 0, col = k;
    while (col >= 0)
    {
      System.out.print(matrix[row][col] + " ");
      row++;
      col--;
    }
  }
 
  // printing elements below
  // second diagonal
  for (int j = 1; j < n; j++)
  {
    // traversing downwards starting
    // from last column
    int col = n - 1, row = j;
    while (row < n)
    {
      System.out.print(matrix[row][col] + " ");
      row++;
      col--;
    }
  }
}
 
// Driver code
public static void main(String[] args)
{
  int[][] matrix = {{1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}};
  int n = 3;
  printMatrixDiagonallyDown(matrix, n);
}
}
 
// This code is contributed by Rajput-Ji


Python 3




# Python 3 program to print matrix downward
 
def printMatrixDiagonallyDown(matrix,n):
    # printing elements above and on
    # second diagonal
    for k in range(n):
        # traversing downwards starting
        # from first row
        row = 0
        col = k
        while (col >= 0):
            print(matrix[row][col],end = " ")
            row += 1
            col -= 1
 
    # printing elements below second
    # diagonal
    for j in range(1,n):
        # traversing downwards starting
        # from last column
        col = n - 1
        row = j
        while (row < n):
            print(matrix[row][col],end = " ")
            row += 1
            col -= 1
 
if __name__ == '__main__':
    matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
    n = 3
    printMatrixDiagonallyDown(matrix, n)
 
# This code is contributed by Surendra_Gangwar


C#




// C# program to print
// matrix downward
using System;
class GFG{
static void printMatrixDiagonallyDown(int[,] matrix,
                                      int n)
{
  // printing elements above and on
  // second diagonal
  for (int k = 0; k < n; k++)
  {
    // traversing downwards
    // starting from first row
    int row = 0, col = k;
     
    while (col >= 0)
    {
      Console.Write(matrix[row,col] + " ");
      row++;
      col--;
    }
  }
 
  // printing elements below
  // second diagonal
  for (int j = 1; j < n; j++)
  {
    // traversing downwards starting
    // from last column
    int col = n - 1, row = j;
    while (row < n)
    {
      Console.Write(matrix[row,col] + " ");
      row++;
      col--;
    }
  }
}
 
// Driver code
public static void Main(String[] args)
{
  int[,] matrix = {{1, 2, 3},
                   {4, 5, 6},
                   {7, 8, 9}};
  int n = 3;
  printMatrixDiagonallyDown(matrix, n);
}
}
 
// This code is contributed by Amit Katiyar


Javascript




<script>
 
// JavaScript program to print
// matrix downward
function printMatrixDiagonallyDown(matrix,n)
{
// printing elements above and on
// second diagonal
for (let k = 0; k < n; k++)
{
    // traversing downwards
    // starting from first row
    let row = 0, col = k;
    while (col >= 0)
    {
    document.write(matrix[row][col] + " ");
    row++;
    col--;
    }
}
 
// printing elements below
// second diagonal
for (let j = 1; j < n; j++)
{
    // traversing downwards starting
    // from last column
    let col = n - 1, row = j;
    while (row < n)
    {
    document.write(matrix[row][col] + " ");
    row++;
    col--;
    }
}
}
 
// Driver code
 
let matrix = [[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]];
let n = 3;
printMatrixDiagonallyDown(matrix, n);
 
// This code is contributed by sravan kumar
 
</script>


Output

1 2 4 3 5 7 6 8 9 

Complexity Analysis:

  • Time Complexity: O(n2
  • Auxiliary Space: 1
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 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments