# Print lower triangular matrix pattern from given array

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 ``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

 ` \$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

 ``

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)

Previous
Next