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
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 array of numbers and insert all the numbers of array of i-th index into two-dimensional array until it becomes zero.
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.
- Print a given matrix in spiral form
- Print unique rows in a given boolean matrix
- Print all possible paths from top left to bottom right of a mXn matrix
- Print all elements in sorted order from row and column wise sorted matrix
- Print n x n spiral matrix using O(1) extra space
- Print maximum sum square sub-matrix of given size
- Print shortest path to print a string on screen
- Print K'th element in spiral form of matrix
- Print matrix in diagonal pattern
- Print cells with same rectangular sums in a matrix
- Print a matrix in alternate manner (left to right then right to left)
- Print matrix in antispiral form
- Print a given matrix in reverse spiral form
- Program to print Lower triangular and Upper triangular matrix of an array
- Print all n digit patterns formed by mobile Keypad
- Print a given matrix in zigzag form
- Print matrix in snake pattern
- Print matrix after applying increment operations in M ranges
- Print a matrix in a spiral form starting from a point
- Print all palindromic paths from top left to bottom right in a matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.