Print matrix in zig-zag fashion
Last Updated :
19 Aug, 2022
Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure.
Example:
Input:
1 2 3
4 5 6
7 8 9
Output:
1 2 4 7 5 3 6 8 9
Approach of C++ code
The approach is simple. Just simply iterate over every diagonal elements one at a time and change the direction according to the previous match.
Approach of Python3 code
This approach is simple. While travelling the matrix in the usual fashion, on basis of parity of the sum of the indices of the element, add that particular element to the list either at the beginning or at the end if sum of i and j is either even or odd respectively. Print the solution list as it is.
Implementation:
C++
#include <iostream>
using namespace std;
#define C 3
void zigZagMatrix( int arr[][C], int n, int m)
{
int row = 0, col = 0;
bool row_inc = 0;
int mn = min(m, n);
for ( int len = 1; len <= mn; ++len) {
for ( int i = 0; i < len; ++i) {
cout << arr[row][col] << " " ;
if (i + 1 == len)
break ;
if (row_inc)
++row, --col;
else
--row, ++col;
}
if (len == mn)
break ;
if (row_inc)
++row, row_inc = false ;
else
++col, row_inc = true ;
}
if (row == 0) {
if (col == m - 1)
++row;
else
++col;
row_inc = 1;
}
else {
if (row == n - 1)
++col;
else
++row;
row_inc = 0;
}
int MAX = max(m, n) - 1;
for ( int len, diag = MAX; diag > 0; --diag) {
if (diag > mn)
len = mn;
else
len = diag;
for ( int i = 0; i < len; ++i) {
cout << arr[row][col] << " " ;
if (i + 1 == len)
break ;
if (row_inc)
++row, --col;
else
++col, --row;
}
if (row == 0 || col == m - 1) {
if (col == m - 1)
++row;
else
++col;
row_inc = true ;
}
else if (col == 0 || row == n - 1) {
if (row == n - 1)
++col;
else
++row;
row_inc = false ;
}
}
}
int main()
{
int matrix[][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
zigZagMatrix(matrix, 3, 3);
return 0;
}
|
Java
class GFG {
static final int C = 3 ;
static void zigZagMatrix( int arr[][], int n, int m) {
int row = 0 , col = 0 ;
boolean row_inc = false ;
int mn = Math.min(m, n);
for ( int len = 1 ; len <= mn; ++len) {
for ( int i = 0 ; i < len; ++i) {
System.out.print(arr[row][col] + " " );
if (i + 1 == len)
break ;
if (row_inc) {
++row;
--col;
} else {
--row;
++col;
}
}
if (len == mn)
break ;
if (row_inc) {
++row;
row_inc = false ;
} else {
++col;
row_inc = true ;
}
}
if (row == 0 ) {
if (col == m - 1 )
++row;
else
++col;
row_inc = true ;
} else {
if (row == n - 1 )
++col;
else
++row;
row_inc = false ;
}
int MAX = Math.max(m, n) - 1 ;
for ( int len, diag = MAX; diag > 0 ; --diag) {
if (diag > mn)
len = mn;
else
len = diag;
for ( int i = 0 ; i < len; ++i) {
System.out.print(arr[row][col] + " " );
if (i + 1 == len)
break ;
if (row_inc) {
++row;
--col;
} else {
++col;
--row;
}
}
if (row == 0 || col == m - 1 ) {
if (col == m - 1 )
++row;
else
++col;
row_inc = true ;
}
else if (col == 0 || row == n - 1 ) {
if (row == n - 1 )
++col;
else
++row;
row_inc = false ;
}
}
}
public static void main(String[] args) {
int matrix[][] = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 }};
zigZagMatrix(matrix, 3 , 3 );
}
}
|
Python3
matrix = [
[ 1 , 2 , 3 ,],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ],
]
rows = 3
columns = 3
solution = [[] for i in range (rows + columns - 1 )]
for i in range (rows):
for j in range (columns):
sum = i + j
if ( sum % 2 = = 0 ):
solution[ sum ].insert( 0 ,matrix[i][j])
else :
solution[ sum ].append(matrix[i][j])
for i in solution:
for j in i:
print (j,end = " " )
|
C#
using System;
class GFG {
static int C = 3;
static void zigZagMatrix( int [, ] arr, int n, int m)
{
int row = 0, col = 0;
bool row_inc = false ;
int mn = Math.Min(m, n);
for ( int len = 1; len <= mn; ++len) {
for ( int i = 0; i < len; ++i) {
Console.Write(arr[row, col] + " " );
if (i + 1 == len)
break ;
if (row_inc) {
++row;
--col;
}
else {
--row;
++col;
}
}
if (len == mn)
break ;
if (row_inc) {
++row;
row_inc = false ;
}
else {
++col;
row_inc = true ;
}
}
if (row == 0) {
if (col == m - 1)
++row;
else
++col;
row_inc = true ;
}
else {
if (row == n - 1)
++col;
else
++row;
row_inc = false ;
}
int MAX = Math.Max(m, n) - 1;
for ( int len, diag = MAX; diag > 0; --diag) {
if (diag > mn)
len = mn;
else
len = diag;
for ( int i = 0; i < len; ++i) {
Console.Write(arr[row, col] + " " );
if (i + 1 == len)
break ;
if (row_inc) {
++row;
--col;
}
else {
++col;
--row;
}
}
if (row == 0 || col == m - 1) {
if (col == m - 1)
++row;
else
++col;
row_inc = true ;
}
else if (col == 0 || row == n - 1) {
if (row == n - 1)
++col;
else
++row;
row_inc = false ;
}
}
}
public static void Main()
{
int [, ] matrix = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
zigZagMatrix(matrix, 3, 3);
}
}
|
PHP
<?php
$C = 3;
function zigZagMatrix( $arr ,
$n , $m )
{
$row = 0; $col = 0;
$row_inc = false;
$mn = min( $m , $n );
for ( $len = 1;
$len <= $mn ; $len ++)
{
for ( $i = 0;
$i < $len ; $i ++)
{
echo ( $arr [ $row ][ $col ]. " " );
if ( $i + 1 == $len )
break ;
if ( $row_inc )
{
$row ++; $col --;
}
else
{
$row --; $col ++;
}
}
if ( $len == $mn )
break ;
if ( $row_inc )
{
++ $row ; $row_inc = false;
}
else
{
++ $col ; $row_inc = true;
}
}
if ( $row == 0)
{
if ( $col == $m - 1)
++ $row ;
else
++ $col ;
$row_inc = 1;
}
else
{
if ( $row == $n - 1)
++ $col ;
else
++ $row ;
$row_inc = 0;
}
$MAX = max( $m , $n ) - 1;
for ( $len , $diag = $MAX ;
$diag > 0; -- $diag )
{
if ( $diag > $mn )
$len = $mn ;
else
$len = $diag ;
for ( $i = 0;
$i < $len ; ++ $i )
{
echo ( $arr [ $row ][ $col ] . " " );
if ( $i + 1 == $len )
break ;
if ( $row_inc )
{
++ $row ; -- $col ;
}
else
{
++ $col ; -- $row ;
}
}
if ( $row == 0 ||
$col == $m - 1)
{
if ( $col == $m - 1)
++ $row ;
else
++ $col ;
$row_inc = true;
}
else if ( $col == 0 ||
$row == $n - 1)
{
if ( $row == $n - 1)
++ $col ;
else
++ $row ;
$row_inc = false;
}
}
}
$matrix = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
zigZagMatrix( $matrix , 3, 3);
?>
|
Javascript
var n = 3, m = 3;
var a = [[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]];
var zigZagMatrix= function (arr, n, m)
{
var row = 0;
var col = 0;
var row_inc = 0;
var mn = Math.min(m, n);
for ( var len = 1; len <= mn; ++len) {
for ( var i = 0; i < len; ++i) {
console.log(arr[row][col]);
console.log( " " );
if (i + 1 == len)
break ;
if (row_inc)
++row, --col;
else
--row, ++col;
}
if (len == mn)
break ;
if (row_inc)
++row, row_inc = false ;
else
++col, row_inc = true ;
}
if (row == 0) {
if (col == m - 1)
++row;
else
++col;
row_inc = 1;
}
else {
if (row == n - 1)
++col;
else
++row;
row_inc = 0;
}
var MAX = Math.max(m, n) - 1;
for ( var len, diag = MAX; diag > 0; --diag) {
if (diag > mn)
len = mn;
else
len = diag;
for ( var i = 0; i < len; ++i) {
console.log(arr[row][col]);
console.log( " " );
if (i + 1 == len)
break ;
if (row_inc)
++row, --col;
else
++col, --row;
}
if (row == 0 || col == m - 1) {
if (col == m - 1)
++row;
else
++col;
row_inc = true ;
}
else if (col == 0 || row == n - 1) {
if (row == n - 1)
++col;
else
++row;
row_inc = false ;
}
}
}
zigZagMatrix(a, 3, 3);
|
Time complexity: O(n*m)
Auxiliary space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...