Program to Print Matrix in Z form

Given a square matrix of order n*n, we need to print elements of the matrix in Z form

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

Input : mat[][] = {5, 19, 8, 7,
                   4, 1, 14, 8,
                   2, 20, 1, 9,
                   1, 2, 55, 4}
Output: 5 19 8 7 14 20 1 2 55 4

https://media.geeksforgeeks.org/wp-content/uploads/Capture-16.png
Complexity O(n)

We need to traverse first row of matrix then second diagonal and then last row.

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print a square matrix in Z form
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
  
// Function to print a square matrix in Z form
void printZform(int mat[][MAX], int n)
{
    // print first row
    for (int i = 0; i < n; i++)
        cout << mat[0][i] << " ";
  
    // Print diagonal
    int i = 1, j = n - 2;
    while (i < n && j >= 0) // print diagonal
    {
        cout << mat[i][j] << " ";
        i++;
        j--;
    }
  
    // Print last row
    for (int i = 1; i < n; i++)
        cout << mat[n - 1][i] << " ";
}
  
// Driver function
int main()
{
    int mat[][MAX] = { { 4, 5, 6, 8 },
                       { 1, 2, 3, 1 },
                       { 7, 8, 9, 4 },
                       { 1, 8, 7, 5 } };
    printZform(mat, 4);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print a
// square matrix in Z form
  
import java.lang.*;
import java.io.*;
  
class GFG {
    public static void diag(int arr[][], int n)
    {
        int i = 0, j, k;
  
        // print first row
        for (j = 0; j < n - 1; j++) {
            System.out.print(arr[i][j] + " ");
        }
  
        // Print diagonal
        k = 1;
        for (i = 0; i < n - 1; i++) {
            for (j = 0; j < n; j++) {
                if (j == n - k) {
                    System.out.print(arr[i][j] + " ");
                    break;
                }
            }
            k++;
        }
        // Print last row
        i = n - 1;
        for (j = 0; j < n; j++)
            System.out.print(arr[i][j] + " ");
  
        System.out.print("\n");
    }
  
    public static void main(String[] args)
    {
        int a[][] = { { 4, 5, 6, 8 },
                      { 1, 2, 3, 1 },
                      { 7, 8, 9, 4 },
                      { 1, 8, 7, 5 } };
  
        diag(a, 4);
    }
}
  
// Code contributed by Mohit Gupta_OMG <(0_o)>

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print a 
# square matrix in Z form
  
arr = [[4, 5, 6, 8], 
        [1, 2, 3, 1], 
        [7, 8, 9, 4], 
        [1, 8, 7, 5]]
  
n = len(arr[0])
                   
i = 0
for j in range(0, n-1):
    print(arr[i][j], end = ' '
          
k = 1
for i in range(0, n):
    for j in range(n, 0, -1):
        if(j == n-k):
            print(arr[i][j], end = ' '
            break
    k+= 1
  
# Print last row
i = n-1
for j in range(0, n):
    print(arr[i][j], end = ' ')
  
# Code contributed by Mohit Gupta_OMG <(0_o)>

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a square 
// matrix in Z form
using System;
  
class GFG {
      
    public static void printZform(int[, ] mat, int n)
    {
  
        int i, j;
          
        // print first row
        for (i = 0; i < n; i++) {
            Console.Write(mat[0, i] + " ");
        }
  
        // Print diagonal
        i = 1;
        j = n - 2;
      
        while (i < n && j >= 0) // print diagonal
        {
            Console.Write(mat[i,j] + " ");
            i++;
            j--;
        }
      
        // Print last row
        for (i = 1; i < n; i++)
            Console.Write(mat[n - 1,i] + " ");
    }
  
    // Driver code
    public static void Main()
    {
        int[, ] mat = { { 4, 5, 6, 8 },
                    { 1, 2, 3, 1 },
                    { 7, 8, 9, 4 },
                    { 1, 8, 7, 5 } };
  
        printZform(mat, 4);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print a 
// square matrix in Z form
  
$MAX = 100;
  
// Function to print a 
// square matrix in Z form
function printZform( $mat, $n)
{
      
    // print first row
    for($i = 0; $i < $n; $i++)
        echo $mat[0][$i] , " ";
  
    // Print diagonal
    $i = 1;$j = $n - 2;
      
    // print diagonal
    while ($i < $n and $j >= 0) 
    {
        echo $mat[$i][$j] , " ";
        $i++;
        $j--;
    }
  
    // Print last row
    for ( $i = 1; $i < $n; $i++)
        echo $mat[$n - 1][$i] , " ";
}
      
    // Driver Code
    $mat = array(array(4, 5, 6, 8),
                 array(1, 2, 3, 1),
                 array(7, 8, 9, 4),
                 array(1, 8, 7, 5));
    printZform($mat, 4);
      
// This code is contributed by anuj_67.
?>

chevron_right



Output:

 4 5 6 8 3 8 1 8 7 5 

This article is contributed by R_Raj. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.