Print a matrix in Reverse Wave Form
Given a matrix, print it in Reverse Wave Form.
Examples :
Input : 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output : 4 8 12 16 15 11 7 3 2 6 10 14 13 9 5 1
Input : 1 9 4 10
3 6 90 11
2 30 85 72
6 31 99 15
Output : 10 11 72 15 99 85 90 4 9 6 30 31 6 2 3 1
Approach :To get the reverse wave form for a given matrix, we first print the elements of the last column of the matrix in downward direction then print the elements of the 2nd last column in the upward direction, then print the elements in third last column in downward direction and so on. For example 1, the flow goes like :
Below is the implementation to print reverse wave form of a matrix :
C++
#include<bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void WavePrint( int m, int n, int arr[R][C])
{
int i, j = n - 1, wave = 1;
while (j >= 0) {
if (wave == 1) {
for (i = 0; i < m; i++)
cout << arr[i][j] << " " ;
wave = 0;
j--;
}
else {
for (i = m - 1; i >= 0; i--)
cout << arr[i][j] << " " ;
wave = 1;
j--;
}
}
}
int main()
{
int arr[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
WavePrint(R, C, arr);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int R = 4 ;
static int C = 4 ;
static void WavePrint( int m, int n, int arr[][])
{
int i, j = n - 1 , wave = 1 ;
while (j >= 0 )
{
if (wave == 1 )
{
for (i = 0 ; i < m; i++)
System.out.print(arr[i][j] + " " );
wave = 0 ;
j--;
}
else {
for (i = m - 1 ; i >= 0 ; i--)
System.out.print( arr[i][j] + " " );
wave = 1 ;
j--;
}
}
}
public static void main (String[] args)
{
int arr[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
WavePrint(R, C, arr);
}
}
|
Python3
R = 4
C = 4
def wavePrint(m, n, arr):
j = n - 1
wave = 1
while j > = 0 :
if wave = = 1 :
for i in range (m):
print (arr[i][j], end = " " ),
wave = 0
j - = 1
else :
for i in range (m - 1 , - 1 , - 1 ):
print (arr[i][j], end = " " ),
wave = 1
j - = 1
arr = [ [ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ] ]
wavePrint(R, C, arr)
|
C#
using System;
class GFG {
static int R = 4;
static int C = 4;
static void WavePrint( int m, int n, int [,]arr)
{
int i, j = n - 1, wave = 1;
while (j >= 0)
{
if (wave == 1) {
for (i = 0; i < m; i++)
Console.Write(arr[i,j] + " " );
wave = 0;
j--;
}
else {
for (i = m - 1; i >= 0; i--)
Console.Write( arr[i,j] + " " );
wave = 1;
j--;
}
}
}
public static void Main ()
{
int [,]arr = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
WavePrint(R, C, arr);
}
}
|
PHP
<?php
$R = 4;
$C = 4;
function WavePrint( $m , $n , $arr )
{
global $R ;
global $C ;
$i ; $j = $n - 1; $wave = 1;
while ( $j >= 0)
{
if ( $wave == 1)
{
for ( $i = 0; $i < $m ; $i ++)
echo $arr [ $i ][ $j ] , " " ;
$wave = 0;
$j --;
}
else
{
for ( $i = $m - 1; $i >= 0; $i --)
echo $arr [ $i ][ $j ] , " " ;
$wave = 1;
$j --;
}
}
}
$arr = array ( array (1, 2, 3, 4),
array (5, 6, 7, 8),
array (9, 10, 11, 12),
array (13, 14, 15, 16));
WavePrint( $R , $C , $arr );
?>
|
Javascript
<script>
R = 4
C = 4
function WavePrint(m, n, arr)
{
var i, j = n - 1, wave = 1;
while (j >= 0)
{
if (wave == 1)
{
for (i = 0; i < m; i++)
document.write( arr[i][j] + " " );
wave = 0;
j--;
}
else
{
for (i = m - 1; i >= 0; i--)
document.write( arr[i][j] + " " );
wave = 1;
j--;
}
}
}
var arr = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ];
WavePrint(R, C, arr);
</script>
|
Output
4 8 12 16 15 11 7 3 2 6 10 14 13 9 5 1
Complexity Analysis
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...