Arrange numbers 1 to N^2 in a Zig-Zag Matrix in ascending order Last Updated : 19 Apr, 2021 Comments Improve Suggest changes Like Article Like Report Given a positive integer N, the task is to print an N × N zig-zag matrix consisting of numbers from 1 to N2, such that the ZigZag traversal of the matrix yields the number in ascending order.Examples: Input: N = 3 Output: 1 2 4 3 5 7 6 8 9 Explanation: Input: N = 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16 Approach: The required matrix can be broken down into two right-angled triangles. An upside-down right-angled triangle(considered as an upper triangle).A normal right-angled triangle(considered as a lower triangle). The idea is to iterate two nested loops to fill the upper triangle with respective values. Then iterate two nested loop again to fill the lower triangle with respective values. After the above two operations print the desired matrix.Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the pattern void printPattern(int n) { // N * N matrix to store the // values int arr[n][n]; arr[0][0] = 1; // Fill the values of // upper triangle for (int i = 0; i < n; i++) { if (i > 0) { arr[i][0] = arr[i - 1][0] + i + 1; } for (int j = 1; j < n - i; j++) { arr[i][j] = arr[i][j - 1] + i + j; } } // Fill the values of // lower triangle arr[1][n - 1] = arr[n - 1][0] + 1; int div = 0; for (int i = 2; i < n; i++) { div = n - 2; for (int j = n - i; j < n; j++) { if (j == n - i) { arr[i][j] = arr[i - 1][j + 1] + 1; } else { arr[i][j] = arr[i][j - 1] + div; div--; } } } // Print the array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << arr[i][j] << " "; } cout << "\n"; } } // Driver Code int main() { // Given size of matrix int N = 4; // Function Call printPattern(N); return 0; } Java // Java program for // the above approach import java.util.*; class GFG{ // Function to print the pattern static void printPattern(int n) { // N * N matrix to store the // values int [][]arr = new int[n][n]; arr[0][0] = 1; // Fill the values of // upper triangle for (int i = 0; i < n; i++) { if (i > 0) { arr[i][0] = arr[i - 1][0] + i + 1; } for (int j = 1; j < n - i; j++) { arr[i][j] = arr[i][j - 1] + i + j; } } // Fill the values of // lower triangle arr[1][n - 1] = arr[n - 1][0] + 1; int div = 0; for (int i = 2; i < n; i++) { div = n - 2; for (int j = n - i; j < n; j++) { if (j == n - i) { arr[i][j] = arr[i - 1][j + 1] + 1; } else { arr[i][j] = arr[i][j - 1] + div; div--; } } } // Print the array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(arr[i][j] + " "); } System.out.print("\n"); } } // Driver Code public static void main(String[] args) { // Given size of matrix int N = 4; // Function Call printPattern(N); } } // This code is contributed by Princi Singh Python3 # Python3 program for the above approach # Function to print the pattern def printPattern(n): # N * N matrix to store the values arr = [[0 for i in range(n)] for j in range(n)] # Fill the values of upper triangle arr[0][0] = 1 for i in range(n): if i > 0: arr[i][0] = arr[i - 1][0] + i + 1 for j in range(1, n-i): arr[i][j] = arr[i][j - 1] + i + j # Fill the values of lower triangle if n > 1: arr[1][n - 1] = arr[n - 1][0] + 1 div = 0 for i in range(2, n): div = n-2 for j in range(n-i, n): if j == n-i: arr[i][j] = arr[i - 1][j + 1] + 1 else: arr[i][j] = arr[i][j - 1] + div div -= 1 # Print the array for i in range(n): for j in range(n): print(arr[i][j], end=' ') print("") # Driver code # Given size of matrix N = 4 # Function Call printPattern(N) C# // C# program for // the above approach using System; class GFG{ // Function to print the pattern static void printPattern(int n) { // N * N matrix to store the // values int [,]arr = new int[n, n]; arr[0,0] = 1; // Fill the values of // upper triangle for (int i = 0; i < n; i++) { if (i > 0) { arr[i, 0] = arr[i - 1, 0] + i + 1; } for (int j = 1; j < n - i; j++) { arr[i, j] = arr[i, j - 1] + i + j; } } // Fill the values of // lower triangle arr[1, n - 1] = arr[n - 1, 0] + 1; int div = 0; for (int i = 2; i < n; i++) { div = n - 2; for (int j = n - i; j < n; j++) { if (j == n - i) { arr[i, j] = arr[i - 1, j + 1] + 1; } else { arr[i, j] = arr[i, j - 1] + div; div--; } } } // Print the array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(arr[i, j] + " "); } Console.Write("\n"); } } // Driver Code public static void Main(String[] args) { // Given size of matrix int N = 4; // Function Call printPattern(N); } } // This code is contributed by shikhasingrajput JavaScript <script> // Javascript program for // the above approach // Function to print the pattern function printPattern(n) { // N * N matrix to store the // values let arr = new Array(n); // Loop to create 2D array using 1D array for (let i = 0; i < arr.length; i++) { arr[i] = new Array(2); } arr[0][0] = 1; // Fill the values of // upper triangle for (let i = 0; i < n; i++) { if (i > 0) { arr[i][0] = arr[i - 1][0] + i + 1; } for (let j = 1; j < n - i; j++) { arr[i][j] = arr[i][j - 1] + i + j; } } // Fill the values of // lower triangle arr[1][n - 1] = arr[n - 1][0] + 1; let div = 0; for (let i = 2; i < n; i++) { div = n - 2; for (let j = n - i; j < n; j++) { if (j == n - i) { arr[i][j] = arr[i - 1][j + 1] + 1; } else { arr[i][j] = arr[i][j - 1] + div; div--; } } } // Print the array for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { document.write(arr[i][j] + " "); } document.write("<br/>"); } } // Driver code // Given size of matrix let N = 4; // Function Call printPattern(N); // This code is contributed by susmitakundugoaldanga. </script> Output1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16 Time Complexity: O(N2) Auxiliary Space: O(N2) Comment More infoAdvertise with us Next Article Arrange numbers 1 to N^2 in a Zig-Zag Matrix in ascending order K kundudinesh007 Follow Improve Article Tags : Mathematical Matrix Computer Science Fundamentals DSA pattern-printing Traversal +2 More Practice Tags : MathematicalMatrixpattern-printingTraversal Similar Reads Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way) Given two values m and n, fill a matrix of size 'm*n' in a spiral (or circular) fashion (clockwise) with natural numbers from 1 to m*n. Examples: Input : m = 4, n = 4 Output : 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 Input : m = 3, n = 4 Output : 1 2 3 4 10 11 12 5 9 8 7 6 The idea is based on Print a 10 min read Converting an array of integers into Zig-Zag fashion! Let us elaborate the problem a little more. Basically, we are given an array of integers and we need to arrange this array in an order such that 1st element is lesser than 2nd element, 2nd element is greater than 3rd element, 3rd element is lesser than 4th element, 4th element is greater than 5th el 4 min read Construct a square Matrix using digits of given number N based on given pattern Given an integer N, The task is to construct a matrix mat[][] of size M x M ('M' is the number of digits in the given integer) such that each diagonal of the matrix contains the same digit, placed according to the position of the digits in the given integer and then again repeat the steps from the b 6 min read Sorting rows of matrix in ascending order followed by columns in descending order Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples : Input : a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 7 8 9 4 5 6 1 2 3 Input : a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output : 7 8 9 4 5 6 1 2 3 Approach: Travers 10 min read Sorting rows of matrix in descending order followed by columns in ascending order Given a matrix of distinct elements. The task is to sort the rows of matrix in descending order followed by sorting the columns in ascending order. Examples: Input: a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output: 3 2 1 6 5 4 9 8 7 Input: a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output: 3 2 9 min read Rearrange a Linked List in Zig-Zag fashion | Set-2 Given a linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c.. are consecutive data node of linked list. Note that it is not allowed to swap data. Examples: Input: 1->2->3->4 Output: 1->3->2->4 Input: 11-> 13 min read Sort a Matrix in all way increasing order Given a square matrix of order N*N having distinct elements, the task is to sort given matrix in such a way that its rows, columns and both diagonals (diagonal and anti-diagonal) are in increasing order. Examples: Input : arr[3][3] = {1, 4, 2, 3, 5, 6, 9, 7, 8} Output :{1, 2, 3, 4, 5, 6, 7, 8, 9} In 5 min read Minimum cost to sort a matrix of numbers from 0 to n^2 - 1 Given an n x n matrix containing all the numbers in the range 0 to n2-1. The problem is to calculate the total energy required for rearranging all the numbers in the matrix in strictly increasing order, i.e., after the rearrangement, the 1st row contains 'n' numbers from 0 to n-1, then 2nd row from 8 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 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 nu 10 min read Like