Print lower triangular matrix pattern from given array Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Given an array, print the array in 2D form where upper triangle has values 0 and lower triangle has values increasing prefix sizes (First row has prefix of size 1, second row has prefix of size 2, ..)Examples : Input : 1 2 3 4 5 Output : 1 0 0 0 0 1 2 0 0 0 1 2 3 0 0 1 2 3 4 0 1 2 3 4 5 Input : 1 2 3 Output : 1 0 0 1 2 0 1 2 3 C++ // C++ implementation to print // given array in 2D form #include <bits/stdc++.h> using namespace std; // Function to print pattern void printPattern(int arr[], int n) { // array to store the final output int b[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) // if i > j then insert arr[j] // in 2D array b[i][j] if (i >= j) b[i][j] = arr[j]; else // insert zero in array b[i][j] b[i][j] = 0; } // printing pattern in 2-D array i.e, // printing 2D array b[i][j] for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << b[i][j] << " "; cout << endl; } } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr) / sizeof(arr[0]); printPattern(arr, n); return 0; } Java // Java implementation to print // given array in 2D form import java.io.*; class GFG { // Function to print pattern static void printPattern(int arr[], int n) { // array to store the final output int b[][] = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) // if i > j then insert arr[j] // in 2D array b[i][j] if (i >= j) b[i][j] = arr[j]; else // insert zero in array b[i][j] b[i][j] = 0; } // printing pattern in 2-D array i.e, // printing 2D array b[i][j] for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) System.out.print(b[i][j] +" "); System.out.println(); } } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 4, 5 }; int n = arr.length; printPattern(arr, n); } } // This code is contributed by vt_m. Python3 # Python3 implementation to print # given array in 2D form # Function to print pattern def printPattern(arr, n): # array to store the final output b = [[0 for i in range(n)] for i in range(n)] for i in range(0, n): for j in range(0, n): # if i > j then insert arr[j] # in 2D array b[i][j] if (i >= j): b[i][j] = arr[j]; else: # insert zero in array b[i][j] b[i][j] = 0; # printing pattern in 2-D array i.e, # printing 2D array b[i][j] for i in range(0, n): for j in range(0, n): print(b[i][j], end = " "); print(); # Driver code arr = [ 1, 2, 3, 4, 5 ]; n = len(arr); printPattern(arr, n); # This code is contributed by # Shivi_Aggarwal C# // C# implementation to print // given array in 2D form using System; namespace Array { public class GFG { // Function to print pattern static void printPattern(int []arr, int n) { // array to store the final output int[,] b = new int[n,n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) // if i > j then insert arr[j] // in 2D array b[i][j] if (i >= j) b[i,j] = arr[j]; else // insert zero in array b[i][j] b[i,j] = 0; } // printing pattern in 2-D array i.e, // printing 2D array b[i][j] for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write(b[i,j] + " "); Console.WriteLine(); } } // Driver code public static void Main() { int []arr = { 1, 2, 3, 4, 5 }; int n = arr.Length; printPattern(arr, n); } } } // This code is contributed by Sam007 PHP <?php // PHP implementation to print // lower triangular matrix pattern // Function to print pattern function printPattern(array $arr, $n) { // $array to store the // final output $b; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) // if $i > $j then insert $arr[$j] // in 2D $array $b[$i][$j] if ($i >= $j) $b[$i][$j] = $arr[$j]; else // insert zero in // $array $b[$i][$j] $b[$i][$j] = 0; } // printing pattern in 2-D // $array i.e, printing 2D // $array $b[$i][$j] for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) echo $b[$i][$j]." "; echo "\n"; } } // Driver code $arr = array(1, 2, 3, 4, 5); $n = sizeof($arr) / sizeof($arr[0]); printPattern($arr, $n); // This code is contributed by Mithun Kumar ?> JavaScript <script> // JavaScript implementation to print // given array in 2D form // Function to print pattern function printPattern(arr, n) { // array to store the final output var b = Array.from(Array(n), ()=>Array(n)); for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) // if i > j then insert arr[j] // in 2D array b[i][j] if (i >= j) b[i][j] = arr[j]; else // insert zero in array b[i][j] b[i][j] = 0; } // printing pattern in 2-D array i.e, // printing 2D array b[i][j] for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) document.write( b[i][j] + " "); document.write("<br>"); } } // Driver code var arr = [1, 2, 3, 4, 5]; var n = arr.length; printPattern(arr, n); </script> Output: 1 0 0 0 0 1 2 0 0 0 1 2 3 0 0 1 2 3 4 0 1 2 3 4 5 Time complexity: O(n^2) for given n, where n is size of given array Auxiliary space: O(n^2) Comment More infoAdvertise with us Next Article Print lower triangular matrix pattern from given array A Anivesh Tiwari Follow Improve Article Tags : Misc DSA Basic Coding Problems pattern-printing Practice Tags : Miscpattern-printing Similar Reads Convert given lower triangular Matrix to 1D array Given a lower triangular matrix M[][] of dimension N * N, the task is to convert it into a one-dimensional array by storing only non-zero elements. Examples: Input: M[][] = {{1, 0, 0, 0}, {2, 3, 0, 0}, {4, 5, 6, 0}, {7, 8, 9, 10}} Output:Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}Column-wise: {1, 2, 4 13 min read Print concentric rectangular pattern in a 2d matrix Given a positive integer n, print the matrix filled with rectangle pattern as shown below: a a a a a a b b b a a b c b a a b b b a a a a a a where a = n, b = n - 1,c = n - 2 and so on. Examples: Input : n = 4 Output : 4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 13 min read Print matrix in snake pattern from the last column Given a matrix of 2-Dimensional array of n rows and n columns. Print this matrix in snake fashion starting from column n-1 as shown in the figure below . Examples: Input : mat[][] = 1 2 3 4 5 6 7 8 9 Output: 3 2 1 4 5 6 9 8 7 Input: mat[][] = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 3 2 1 5 6 min read 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 Convert given upper triangular Matrix to 1D Array Given an upper triangular matrix M[][] of dimensions N * N, the task is to convert it into an one-dimensional array storing only non-zero elements from the matrix. Examples: Input: M[][] = {{1, 2, 3, 4}, {0, 5, 6, 7}, {0, 0, 8, 9}, {0, 0, 0, 10}}Output: Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Colu 12 min read Swap upper and lower triangular halves of a given Matrix Given a square matrix mat[][] of dimensions N * N, the task is to print the matrix that can be obtained after swapping the laterally inverted images of the upper and lower triangular halves of a given matrix. Consider the matrix mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}The lateral image of the low 13 min read Print matrix in snake pattern Given an n x n matrix. In the given matrix, you have to print the elements of the matrix in the snake pattern.Examples : Input: mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}};Output: 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input: mat[][] = [[1, 2], [3, 4] 4 min read Program for triangular pattern (mirror image around 0) Given the value of n, print the pattern.Examples : Input : n = 5 Output : 0 101 21012 3210123 432101234 Input : n = 7 Output : 0 101 21012 3210123 432101234 54321012345 6543210123456 Below is the program to print the above pattern C++ // C++ Implementation to print the pattern #include <bits/stdc 5 min read Print rectangular pattern with given center Given 3 positive integer c1, c2 and n, where n is size of 2-D square matrix. The task is to print the matrix filled with rectangular pattern having center coordinates c1, c2 such that 0 <= c1, c2 < n. Examples: Input: c1 = 2, c2 = 2, n = 5Output:2 2 2 2 2 2 1 1 1 2 2 1 0 1 2 2 1 1 1 2 2 2 2 2 4 min read Program to print Lower triangular and Upper triangular matrix of an array Prerequisite - Multidimensional Arrays in C / C++Given a two dimensional array, Write a program to print lower triangular matrix and upper triangular matrix. Lower triangular matrix is a matrix which contains elements below principal diagonal including principal diagonal elements and rest of the ele 9 min read Like