Given 1's, 2's, 3's ......k's print them in zig zag way.
Last Updated :
01 Aug, 2022
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
Approach: We make a two-dimensional array to store all the elements in zig-zag way. we will traverse through all the elements of an array of numbers and insert all the numbers of an array of i-th index into a two-dimensional array until it becomes zero.
Implementation:
C++
// CPP program to print given number of 1's,
// 2's, 3's ....k's in zig-zag way.
#include <bits/stdc++.h>
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; i<rows; i++)
{
// for even row.
if (i%2==0)
{
// for each column.
for (int j=0; j<columns 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++;
}
}
// 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<rows;i++)
{
for (int j=0;j<columns;j++)
cout << arr[i][j] << " ";
cout << endl;
}
}
// Driver code for above function.
int main()
{
int rows = 4;
int columns = 5;
int Numbers[] = {3, 4, 2, 2, 3, 1, 5};
ZigZag(rows, columns, Numbers);
return 0;
}
C
// C program to print given number of 1's,
// 2's, 3's ....k's in zig-zag way.
#include <stdio.h>
// 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; 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++)
printf("%d ",arr[i][j]);
printf("\n");
}
}
// Driver code for above function.
int main()
{
int rows = 4;
int columns = 5;
int Numbers[] = {3, 4, 2, 2, 3, 1, 5};
ZigZag(rows, columns, Numbers);
return 0;
}
// This code is contributed by kothavvsaakash.
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<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++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
// Driver function
public static void main(String argc[])
{
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 Sagar Shukla */
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
<?php
// PHP 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.
function ZigZag($rows, $columns, $numbers)
{
$k = 0;
// two-dimensional array
// to store numbers.
$arr = array(array());
for($i = 0; $i < $rows; $i++)
{
// for even row.
if ($i % 2==0)
{
// for each column.
for($j = 0; $j < $columns 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++;
}
}
// 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.
?>
JavaScript
<script>
// JavaScript 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.
function ZigZag(rows, columns, numbers)
{
let k = 0;
// two-dimensional array to store numbers.
let arr = new Array(rows);
for (let i=0; i<rows; i++)
{
arr[i] = new Array(rows);
for (let j=0; j < columns; j++)
{
arr[i][j] = 0;
}
}
for (let i=0; i<rows; i++)
{
// for even row.
if (i%2==0)
{
// for each column.
for (let 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 (let 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 (let i=0;i<rows;i++)
{
for (let j=0;j<columns;j++)
document.write(arr[i][j] + " ");
document.write("</br>");
}
}
let rows = 4;
let columns = 5;
let Numbers = [3, 4, 2, 2, 3, 1, 5];
ZigZag(rows, columns, Numbers);
</script>
Output1 1 1 2 2
4 3 3 2 2
4 5 5 5 6
7 7 7 7 7
Time Complexity: O(N*M), as we are using nested loops for traversing the matrix.
Auxiliary Space: O(N*M), as we are using extra space.
Similar Reads
Print a given matrix in zigzag form 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 // C++ program to print // matrix in zig-zag form #inc
7 min read
Print Concatenation of Zig-Zag String in 'n' Rows Given a string and number of rows 'n'. Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion. Examples: Input: str = "ABCDEFGH" n = 2 Output: "ACEGBDFH" Explanation: Let us write input string in Zig-Zag fashion in 2 rows. A C E G B D F H Now concate
12 min read
Print matrix in zig-zag fashion 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 9Input : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]Output:: 1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16Thi
10 min read
Concatenation of Zig-Zag String in N Rows The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: PAHNAPLSIIGYIR. Therefore, for given string str and an integer N, the t
6 min read
Print pattern using only one loop | Set 1 (Using setw) Print simple patterns like below using single line of code under loop. Examples: Input : 5Output : * ** *** *********Input : 6Output : * ** *** **** ***********setw(n) Creates n columns and fills these n columns from right. We fill i of them with a given character, here we create a string with i ast
4 min read