# Given 1’s, 2’s, 3’s ……k’s print them in zig zag way.

Given number of rows and columns. And given number of 1’s, 2’s, 3’s ……k’s which needs to be printed. Print them in a zig-zag way.
It is guaranteed that n*m = number of 1’s + 2’s + 3’s + …… + k’s

Examples:

```Input :  2 3
2 1 2 1
Output : 1 1 2
4 3 3
Explanation :
Here number of rows are 2 and number of columns are 3
and number of 1's are 2
number of 2's are 1
number of 3's are 2
number of 4's are 1
-----------
| 1 | 1 | 2 |
| 3 | 3 | 4 |
-----------

Input :  4 3
2 4 3 1 2
Output : 1 1 2
2 2 2
3 3 3
5 5 4
Explanation :
Here number of rows are 4 and number of columns are 3
and number of 1's are 2
number of 2's are 4 [Note that 2s are printed in]
number of 3's are 3 [zig zag manner]
number of 4's are 1
number of 5's are 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: We make a two-dimensional array to store all the elements in zig-zag way. we will traverse through all the elements of array of numbers and insert all the numbers of array of i-th index into two-dimensional array until it becomes zero.

## C++

 `// CPP program to print  given number of 1's,  ` `// 2's, 3's ....k's in  zig-zag way. ` `#include ` `using` `namespace` `std; ` ` `  `// function that prints  given number of 1's,  ` `// 2's, 3's ....k's in zig-zag way. ` `void` `ZigZag(``int` `rows, ``int` `columns, ``int` `numbers[]) ` `{ ` `    ``int` `k = 0; ` `     `  `    ``// two-dimensional array to store numbers.  ` `    ``int` `arr[rows][columns]; ` `     `  `    ``for` `(``int` `i=0; i0; j++) ` `            ``{ ` `                ``// storing element. ` `                ``arr[i][j] = k+1; ` ` `  `                ``// decrement element at  ` `                ``// kth index.  ` `                ``numbers[k]--; ` `                 `  `                ``// if array contains zero ` `                ``// then increment index to  ` `                ``// make this next index ` `                ``if` `(numbers[k] == 0) ` `                    ``k++; ` `            ``} ` `        ``} ` `         `  `        ``// for odd row. ` `        ``else` `        ``{ ` `            ``// for each column. ` `            ``for` `(``int` `j=columns-1; j>=0 and  ` `                ``numbers[k]>0; j--) ` `            ``{ ` `                ``// storing element. ` `                ``arr[i][j] = k+1; ` `                 `  `                ``// decrement element  ` `                ``// at kth index. ` `                ``numbers[k]--; ` `                 `  `                ``// if array contains zero then  ` `                ``// increment index to make this  ` `                ``// next index. ` `                ``if` `(numbers[k]==0) ` `                    ``k++; ` `            ``} ` `        ``} ` `    ``} ` `     `  `    ``// printing the stored elements. ` `    ``for` `(``int` `i=0;i

## Java

 `// Java program to print given ` `// number of 1's, 2's, 3's ....k's  ` `// in zig-zag way. ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GfG{ ` `     `  `    ``// function that prints given number of 1's,  ` `    ``// 2's, 3's ....k's in zig-zag way. ` `    ``public` `static` `void` `ZigZag(``int` `rows,  ` `                              ``int` `columns,  ` `                              ``int` `numbers[]) ` `    ``{ ` `        ``int` `k = ``0``; ` `     `  `        ``// two-dimensional array to store numbers.  ` `        ``int``[][] arr = ``new` `int``[rows][columns]; ` `     `  `        ``for` `(``int` `i=``0``; i``0``; j++) ` `                ``{ ` `                    ``// storing element. ` `                    ``arr[i][j] = k+``1``; ` ` `  `                    ``// decrement element at  ` `                    ``// kth index.  ` `                    ``numbers[k]--; ` `                 `  `                    ``// if array contains zero ` `                    ``// then increment index to  ` `                    ``// make this next index ` `                    ``if` `(numbers[k] == ``0``) ` `                        ``k++; ` `                ``} ` `            ``} ` `         `  `            ``// for odd row. ` `            ``else` `            ``{ ` `                ``// for each column. ` `                ``for` `(``int` `j=columns-``1``; j>=``0` `&& ` `                    ``numbers[k]>``0``; j--) ` `                ``{ ` `                    ``// storing element. ` `                    ``arr[i][j] = k+``1``; ` `                 `  `                    ``// decrement element  ` `                    ``// at kth index. ` `                    ``numbers[k]--; ` `                 `  `                    ``// if array contains zero then  ` `                    ``// increment index to make this  ` `                    ``// next index. ` `                    ``if` `(numbers[k]==``0``) ` `                        ``k++; ` `                ``} ` `            ``} ` `        ``} ` `     `  `        ``// printing the stored elements. ` `        ``for` `(``int` `i=``0``;i

## Python 3

 `# Python3 program to print given number of 1's,  ` `# 2's, 3's ....k's in zig-zag way.  ` ` `  `# function that prints given number of 1's,  ` `# 2's, 3's ....k's in zig-zag way.  ` `def` `ZigZag(rows, columns, numbers): ` `    ``k ``=` `0` `     `  `    ``# two-dimensional array to store numbers. ` `    ``arr ``=` `[[``0` `for` `i ``in` `range``(columns)] ``for` `j ``in` `range``(rows)]  ` `    ``for` `i ``in` `range``(rows): ` `         `  `        ``# for even row. ` `        ``if` `(i ``%` `2` `=``=` `0``): ` `             `  `            ``# for each column. ` `            ``j ``=` `0` `            ``while` `j < columns ``and` `numbers[k] > ``0``: ` `                 `  `                ``# storing element. ` `                ``arr[i][j] ``=` `k ``+` `1` `                 `  `                ``# decrement element at ` `                ``# kth index. ` `                ``numbers[k] ``-``=` `1`     `                 `  `                ``# if array contains zero ` `                ``# then increment index to ` `                ``# make this next index ` `                ``if` `numbers[k] ``=``=` `0``: ` `                    ``k ``+``=` `1` `                ``j ``+``=` `1` `        ``# for odd row. ` `        ``else``: ` `             `  `            ``# for each column. ` `            ``j ``=` `columns``-``1` `            ``while` `j>``=``0` `and` `numbers[k]>``0``: ` `                 `  `                ``# storing element. ` `                ``arr[i][j] ``=` `k``+``1` `                 `  `                ``# decrement element ` `                ``# at kth index. ` `                ``numbers[k] ``-``=` `1` `                 `  `                ``# if array contains zero then ` `                ``# increment index to make this ` `                ``# next index. ` `                ``if` `numbers[k] ``=``=` `0``: ` `                    ``k ``+``=` `1` `                ``j ``-``=` `1` `     `  `    ``# printing the stored elements.  ` `    ``for` `i ``in` `arr: ` `        ``for` `j ``in` `i: ` `            ``print``(j, end ``=``" "``) ` `        ``print``() ` ` `  `# Driver code ` `rows ``=` `4``;  ` `columns ``=` `5``;  ` `Numbers ``=` `[``3``, ``4``, ``2``, ``2``, ``3``, ``1``, ``5``]  ` `ZigZag(rows, columns, Numbers) ` ` `  `# This code is contributed by ` `# Rajnis09 `

## C#

 `// C# program to print given ` `// number of 1's, 2's, 3's ....k's  ` `// in zig-zag way. ` `using` `System; ` ` `  `public` `class` `GfG{ ` `     `  `    ``// function that prints given number of 1's,  ` `    ``// 2's, 3's ....k's in zig-zag way. ` `    ``public` `static` `void` `ZigZag(``int` `rows,  ` `                            ``int` `columns,  ` `                            ``int` `[]numbers) ` `    ``{ ` `        ``int` `k = 0; ` `     `  `        ``// two-dimensional array to store numbers.  ` `        ``int``[,] arr = ``new` `int``[rows,columns]; ` `     `  `        ``for` `(``int` `i = 0; i < rows; i++) ` `        ``{ ` `            ``// for even row. ` `            ``if` `(i % 2 == 0) ` `            ``{ ` `                ``// for each column. ` `                ``for` `(``int` `j = 0; j < columns && ` `                        ``numbers[k] > 0; j++) ` `                ``{ ` `                    ``// storing element. ` `                    ``arr[i,j] = k + 1; ` ` `  `                    ``// decrement element at  ` `                ``// kth index.  ` `                    ``numbers[k]--; ` `                 `  `                    ``// if array contains zero ` `                    ``// then increment index to  ` `                    ``// make this next index ` `                    ``if` `(numbers[k] == 0) ` `                        ``k++; ` `                ``} ` `            ``} ` `         `  `            ``// for odd row. ` `            ``else` `            ``{ ` `                ``// for each column. ` `                ``for` `(``int` `j = columns - 1; j >= 0 && ` `                    ``numbers[k] > 0; j--) ` `                ``{ ` `                    ``// storing element. ` `                    ``arr[i,j] = k + 1; ` `                 `  `                    ``// decrement element  ` `                    ``// at kth index. ` `                    ``numbers[k]--; ` `                 `  `                    ``// if array contains zero then  ` `                    ``// increment index to make this  ` `                    ``// next index. ` `                    ``if` `(numbers[k] == 0) ` `                        ``k++; ` `                ``} ` `            ``} ` `        ``} ` `     `  `        ``// printing the stored elements. ` `        ``for` `(``int` `i = 0;i < rows; i++) ` `        ``{ ` `            ``for` `(``int` `j = 0; j < columns; j++) ` `            ``Console.Write(arr[i, j] + ``" "``); ` `             `  `            ``Console.WriteLine(); ` `        ``} ` `    ``} ` `     `  `    ``// Driver function  ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `rows = 4; ` `        ``int` `columns = 5; ` `        ``int` `[]Numbers = ``new` `int``[]{3, 4, 2, ` `                                ``2, 3, 1, 5}; ` `        ``ZigZag(rows, columns, Numbers);  ` `    ``} ` `} ` ` `  `/* This code is contributed by vt_m*/`

## PHP

 ` 0; ``\$j``++) ` `            ``{ ` `                 `  `                ``// storing element. ` `                ``\$arr``[``\$i``][``\$j``] = ``\$k` `+ 1; ` ` `  `                ``// decrement element at  ` `                ``// kth index.  ` `                ``\$numbers``[``\$k``]--; ` `                 `  `                ``// if array contains zero ` `                ``// then increment index to  ` `                ``// make this next index ` `                ``if` `(``\$numbers``[``\$k``] == 0) ` `                    ``\$k``++; ` `            ``} ` `        ``} ` `         `  `        ``// for odd row. ` `        ``else` `        ``{ ` `             `  `            ``// for each column. ` `            ``for``(``\$j` `= ``\$columns` `- 1; ``\$j` `>= 0 ``and` `                       ``\$numbers``[``\$k``] > 0; ``\$j``--) ` `            ``{ ` `                 `  `                ``// storing element. ` `                ``\$arr``[``\$i``][``\$j``] = ``\$k` `+ 1; ` `                 `  `                ``// decrement element  ` `                ``// at kth index. ` `                ``\$numbers``[``\$k``]--; ` `                 `  `                ``// if array contains zero then  ` `                ``// increment index to make this  ` `                ``// next index. ` `                ``if` `(``\$numbers``[``\$k``]==0) ` `                    ``\$k``++; ` `            ``} ` `        ``} ` `    ``} ` `     `  `    ``// printing the stored elements. ` `    ``for``(``\$i` `= 0; ``\$i` `< ``\$rows``;``\$i``++) ` `    ``{ ` `        ``for``(``\$j` `= 0; ``\$j` `< ``\$columns``; ``\$j``++) ` `            ``echo` `\$arr``[``\$i``][``\$j``] , ``" "``; ` `             `  `        ``echo` `"\n"``; ` `    ``} ` `} ` ` `  `    ``// Driver Code ` `    ``\$rows` `= 4; ` `    ``\$columns` `= 5; ` `    ``\$Numbers` `= ``array``(3, 4, 2, 2, 3, 1, 5); ` `    ``ZigZag(``\$rows``, ``\$columns``,``\$Numbers``);  ` `     `  `// This code is contributed by anuj_67. ` `?> `

Output:

```1 1 1 2 2
4 3 3 2 2
4 5 5 5 6
7 7 7 7 7
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, Rajnis09

Article Tags :
Practice Tags :

1

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