Print a given matrix in zigzag form
Last Updated :
10 Nov, 2022
Given a 2D array, print it in zigzag form. Examples :
Input :
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Output :
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16
Input :
10 24 32
50 6 17
99 10 11
Output :
10 24 32 17 6 50 99 10 11
CPP
#include <iostream>
using namespace std;
const int MAX = 100;
void printZigZag( int row, int col, int a[][MAX])
{
int evenRow = 0;
int oddRow = 1;
while (evenRow<row)
{
for ( int i=0;i<col;i++)
{
cout<<a[evenRow][i] << " " ;
}
evenRow = evenRow + 2;
if (oddRow < row)
{
for ( int i=col-1; i>=0; i--)
{
cout<<a[oddRow][i] << " " ;
}
}
oddRow = oddRow + 2;
}
}
int main() {
int r = 3, c = 5;
int mat[][MAX] = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}};
printZigZag(r , c , mat);
return 0;
}
|
Java
public class GFG
{
static void printZigZag( int row, int col, int a[][])
{
int evenRow = 0 ;
int oddRow = 1 ;
while (evenRow < row)
{
for ( int i = 0 ; i < col; i++)
{
System.out.print(a[evenRow][i] + " " );
}
evenRow = evenRow + 2 ;
if (oddRow < row)
{
for ( int i = col - 1 ; i >= 0 ; i--)
{
System.out.print(a[oddRow][i] + " " );
}
}
oddRow = oddRow + 2 ;
}
}
public static void main(String[] args)
{
int r = 3 , c = 5 ;
int mat[][] = { { 1 , 2 , 3 , 4 , 5 },
{ 6 , 7 , 8 , 9 , 10 },
{ 11 , 12 , 13 , 14 , 15 }};
printZigZag(r , c , mat);
}
}
|
Python3
def printZigZag(row, col, a):
evenRow = 0
oddRow = 1
while evenRow < row:
for i in range (col):
print ( str (a[evenRow][i] ),
end = " " )
evenRow = evenRow + 2
if oddRow < row:
for i in range (col - 1 , - 1 , - 1 ):
print ( str (a[oddRow][i]),
end = " " )
oddRow = oddRow + 2
r = 3
c = 5
mat = [[ 1 , 2 , 3 , 4 , 5 ],
[ 6 , 7 , 8 , 9 , 10 ],
[ 11 , 12 , 13 , 14 , 15 ]];
printZigZag(r , c , mat)
|
C#
using System;
public class GFG {
static void printZigZag( int row, int col, int [, ] a)
{
int evenRow = 0;
int oddRow = 1;
while (evenRow < row) {
for ( int i = 0; i < col; i++) {
Console.Write(a[evenRow, i] + " " );
}
evenRow = evenRow + 2;
if (oddRow < row)
{
for ( int i = col - 1; i >= 0; i--)
{
Console.Write(a[oddRow, i] + " " );
}
}
oddRow = oddRow + 2;
}
}
public static void Main()
{
int r = 3, c = 5;
int [, ] mat = { { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 }
};
printZigZag(r, c, mat);
}
}
|
PHP
<?php
function printZigZag( $row , $col , $a )
{
$evenRow = 0;
$oddRow = 1;
while ( $evenRow < $row )
{
for ( $i = 0; $i < $col ; $i ++)
{
echo $a [ $evenRow ][ $i ], " " ;
}
$evenRow = $evenRow + 2;
if ( $oddRow < $row )
{
for ( $i = $col - 1; $i >= 0; $i --)
{
echo $a [ $oddRow ][ $i ], " " ;
}
}
$oddRow = $oddRow + 2;
}
}
$r = 3; $c = 5;
$mat = array ( array (1, 2, 3, 4, 5),
array (6, 7, 8, 9, 10),
array (11, 12, 13, 14, 15));
printZigZag( $r , $c , $mat );
?>
|
Javascript
var MAX = 100;
var printZigZag = function (row, col, a)
{
var evenRow = 0;
var oddRow = 1;
while (evenRow<row)
{
for ( var i = 0; i < col; i++)
{
document.write(a[evenRow][i]+ " " );
}
evenRow = evenRow + 2;
if (oddRow < row)
{
for ( var i = col - 1; i >= 0; i--)
{
console.log(a[oddRow][i]+ " " );
}
}
oddRow = oddRow + 2;
}
}
var r = 3, c = 5;
var mat = [ [1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]];
printZigZag(r , c , mat);
|
Output
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15
Time Complexity: Time complexity of the above solution is O(row*column).
Space Complexity: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...