# Print Matrix in Wave Form

• Difficulty Level : Medium
• Last Updated : 31 Dec, 2021

Given a matrix mat[][], print it in Wave Form.

Input: mat[][] = {{  1,   2,   3,   4}
{  5,   6,   7,   8}
{  9, 10, 11, 12}
{13, 14, 15, 16}
{17, 18, 19, 20}}
Output: 1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4
Explanation: Output is printed in wave form.

Input: mat[][] = {{1,   9,  4, 10}
{ 3,   6, 90, 11}
{ 2, 30, 85, 72}
{ 6, 31, 99, 15}}
Output: 1 3 2 6 31 30 6 9 4 90 85 99 15 72 11 10

Approach: This problem is implementation-based and has a similar approach as discussed in this article. To get the desired waveform for a given matrix, first, print the elements of the first column of the matrix in the downward direction and then print the elements of the 2nd column in the upward direction, then print the elements in the third column in the downward direction and so on.

Below is the implementation of the above approach:

## C++

 `// C++ program for above approach``#include ``using` `namespace` `std;` `#define R 5``#define C 4` `// Function to print wave``// Form for a given matrix``void` `WavePrint(``int` `m, ``int` `n, ``int` `arr[R][C])``{``    ``// Loop to traverse matrix``    ``for` `(``int` `j = 0; j < n; j++) {` `        ``// If the current column``        ``// is even indexed, print``        ``// it in forward order``        ``if` `(j % 2 == 0) {``            ``for` `(``int` `i = 0; i < m; i++) {``                ``cout << arr[i][j] << ``" "``;``            ``}``        ``}` `        ``// If the current column``        ``// is odd indexed, print``        ``// it in reverse order``        ``else` `{``            ``for` `(``int` `i = m - 1; i >= 0; i--) {``                ``cout << arr[i][j] << ``" "``;``            ``}``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[R][C] = { { 1, 2, 3, 4 },``                      ``{ 5, 6, 7, 8 },``                      ``{ 9, 10, 11, 12 },``                      ``{ 13, 14, 15, 16 },``                      ``{ 17, 18, 19, 20 } };` `    ``WavePrint(R, C, arr);` `    ``return` `0;``}`

## C

 `// C Program for above approach``#include ` `#define R 5``#define C 4` `// Function to print wave``// Form for a given matrix``void` `WavePrint(``int` `m, ``int` `n, ``int` `arr[R][C])``{``    ``// Loop to traverse matrix``    ``for` `(``int` `j = 0; j < n; j++) {` `        ``// If the current column``        ``// is even indexed, print``        ``// it in forward order``        ``if` `(j % 2 == 0) {``            ``for` `(``int` `i = 0; i < m; i++) {``                ``printf``(``"%d "``, arr[i][j]);``            ``}``        ``}` `        ``// If the current column``        ``// is odd indexed, print``        ``// it in reverse order``        ``else` `{``            ``for` `(``int` `i = m - 1; i >= 0; i--) {``                ``printf``(``"%d "``, arr[i][j]);``            ``}``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[R][C] = { { 1, 2, 3, 4 },``                      ``{ 5, 6, 7, 8 },``                      ``{ 9, 10, 11, 12 },``                      ``{ 13, 14, 15, 16 },``                      ``{ 13, 14, 15, 16 } };``    ``WavePrint(R, C, arr);` `    ``return` `0;``}`

## Java

 `// Java program for above approach``import` `java.io.*;``class` `GFG {` `  ``public` `static` `int` `R = ``5``;``  ``public` `static` `int` `C = ``4``;` `  ``// Function to print wave``  ``// Form for a given matrix``  ``public` `static` `void` `WavePrint(``int` `m, ``int` `n, ``int``[][] arr)``  ``{``    ` `    ``// Loop to traverse matrix``    ``for` `(``int` `j = ``0``; j < n; j++) {` `      ``// If the current column``      ``// is even indexed, print``      ``// it in forward order``      ``if` `(j % ``2` `== ``0``) {``        ``for` `(``int` `i = ``0``; i < m; i++) {``          ``System.out.print(arr[i][j] + ``" "``);``        ``}``      ``}` `      ``// If the current column``      ``// is odd indexed, print``      ``// it in reverse order``      ``else` `{``        ``for` `(``int` `i = m - ``1``; i >= ``0``; i--) {``          ``System.out.print(arr[i][j] + ``" "``);``        ``}``      ``}``    ``}``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main (String[] args)``  ``{``    ``int``[][] arr = { { ``1``, ``2``, ``3``, ``4` `},``                   ``{ ``5``, ``6``, ``7``, ``8` `},``                   ``{ ``9``, ``10``, ``11``, ``12` `},``                   ``{ ``13``, ``14``, ``15``, ``16` `},``                   ``{ ``17``, ``18``, ``19``, ``20` `} };` `    ``WavePrint(R, C, arr);``  ``}``}` `// This code is contributed by Shubham Singh`

## Python3

 `# Python code for the above approach``R ``=` `5``C ``=` `4` `# Function to print wave``# Form for a given matrix``def` `WavePrint(m, n, arr):` `    ``# Loop to traverse matrix``    ``for` `j ``in` `range``(n):` `        ``# If the current column``        ``# is even indexed, print``        ``# it in forward order``        ``if` `(j ``%` `2` `=``=` `0``):``            ``for` `i ``in` `range``(m):``                ``print``(arr[i][j], end``=` `" "``)` `        ``# If the current column``        ``# is odd indexed, print``        ``# it in reverse order``        ``else``:``            ``for` `i ``in` `range``(m ``-` `1``, ``-``1``, ``-``1``):``                ``print``(arr[i][j], end``=` `" "``)` `# Driver Code``arr ``=` `[[``1``, ``2``, ``3``, ``4``], [``5``, ``6``, ``7``, ``8``], [``9``, ``10``, ``11``, ``12``], [``13``, ``14``, ``15``, ``16``], [``17``, ``18``, ``19``, ``20``]]` `WavePrint(R, C, arr)` `# This code is contributed by gfgking`

## C#

 `// C# program for above approach``using` `System;` `class` `GFG{` `public` `static` `int` `R = 5;``public` `static` `int` `C = 4;` `// Function to print wave``// Form for a given matrix``public` `static` `void` `WavePrint(``int` `m, ``int` `n, ``int``[,] arr)``{` `    ``// Loop to traverse matrix``    ``for``(``int` `j = 0; j < n; j++)``    ``{``    ` `        ``// If the current column``        ``// is even indexed, print``        ``// it in forward order``        ``if` `(j % 2 == 0)``        ``{``            ``for``(``int` `i = 0; i < m; i++)``            ``{``                ``Console.Write(arr[i, j] + ``" "``);``            ``}``        ``}``    ` `        ``// If the current column``        ``// is odd indexed, print``        ``// it in reverse order``        ``else``        ``{``            ``for``(``int` `i = m - 1; i >= 0; i--)``            ``{``                ``Console.Write(arr[i, j] + ``" "``);``            ``}``        ``}``    ``}``}` `// Driver Code``public` `static` `void` `Main ()``{``    ``int``[,] arr = { { 1, 2, 3, 4 },``                   ``{ 5, 6, 7, 8 },``                   ``{ 9, 10, 11, 12 },``                   ``{ 13, 14, 15, 16 },``                   ``{ 17, 18, 19, 20 } };``    ` `    ``WavePrint(R, C, arr);``}``}` `// This code is contributed by gfgking`

## Javascript

 ``
Output
`1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4 `

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up