Given an integer N, the task is to print the given pattern.
Examples:
Input: 3 Output: 1 2 4 3 5 7 6 8 9 Input: 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16
Approach:
- Create a matrix of size N X N which will store the pattern before printing.
- Store the elements in the upper triangle of the pattern. As observed the row index increases by 1 and column index decreases by 1 as you move down the diagonal.
- Once the upper triangle is completed then store the elements of the lower triangle in similar way as the upper triangle i.e. row index increases by 1 and column index decreases by 1 as you move down the diagonal.
Below is the implementation of the above approach:
C++
// C++ program to print the required pattern#include <bits/stdc++.h>using namespace std;// Function to print the required patternvoid printPattern(int n){ // arr[][] will store the pattern matrix int arr[n][n], k, i, j, p = 1, f; // Store the values for upper triangle // of the pattern for (k = 0; k < n; k++) { j = k; i = 0; while (j >= 0) { arr[i][j] = p; p++; i = i + 1; j = j - 1; } } // Store the values for lower triangle // of the pattern for (k = 1; k < n; k++) { i = k; j = n - 1; f = k; while (j >= f) { arr[i][j] = p; p++; i = i + 1; j = j - 1; } } // Print the pattern for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cout << arr[i][j] << " "; } cout << endl; }}// Driver codeint main(){ int n = 3; printPattern(n); return 0;} |
Java
// Java program to print the required pattern public class GFG{ // Function to print the required pattern static void printPattern(int n) { // arr[][] will store the pattern matrix int arr[][] = new int[n][n] ; int k, i, j, p = 1, f ; // Store the values for upper triangle // of the pattern for (k = 0; k < n; k++) { j = k; i = 0; while (j >= 0) { arr[i][j] = p; p++; i = i + 1; j = j - 1; } } // Store the values for lower triangle // of the pattern for (k = 1; k < n; k++) { i = k; j = n - 1; f = k; while (j >= f) { arr[i][j] = p; p++; i = i + 1; j = j - 1; } } // Print the pattern for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { System.out.print(arr[i][j] + " ") ; } System.out.println() ; } } // Driver code public static void main(String []args) { int n = 3; printPattern(n); } // This code is contributed by Ryuga} |
Python3
# Python 3 program to print the# required pattern# Function to print the required patterndef printPattern(n): # arr[][] will store the pattern matrix arr = [[0 for i in range(n)] for j in range(n)] p = 1 # Store the values for upper # triangle of the pattern for k in range(n): j = k i = 0 while (j >= 0): arr[i][j] = p p += 1 i = i + 1 j = j - 1 # Store the values for lower triangle # of the pattern for k in range(1, n, 1): i = k j = n - 1 f = k while (j >= f): arr[i][j] = p p += 1 i = i + 1 j = j - 1 # Print the pattern for i in range(0, n, 1): for j in range(0, n, 1): print(arr[i][j], end = " ") print("\n", end = "")# Driver codeif __name__ == '__main__': n = 3 printPattern(n)# This code is contributed by# Sanjit_Prasad |
C#
// C# program to print the required pattern using System;public class GFG{ // Function to print the required pattern static void printPattern(int n) { // arr[][] will store the pattern matrix int [,]arr = new int[n,n] ; int k, i, j, p = 1, f ; // Store the values for upper triangle // of the pattern for (k = 0; k < n; k++) { j = k; i = 0; while (j >= 0) { arr[i,j] = p; p++; i = i + 1; j = j - 1; } } // Store the values for lower triangle // of the pattern for (k = 1; k < n; k++) { i = k; j = n - 1; f = k; while (j >= f) { arr[i,j] = p; p++; i = i + 1; j = j - 1; } } // Print the pattern for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { Console.Write(arr[i,j] + " ") ; } Console.WriteLine() ; } } // Driver code public static void Main() { int n = 3; printPattern(n); } // This code is contributed by inder_verma..} |
PHP
<?php// PHP program to print the required pattern // Function to print the required pattern function printPattern($n) { // arr[][] will store the pattern matrix $arr[][] = array($n, $n); $k; $i; $j; $p = 1; $f; // Store the values for upper // triangle of the pattern for ($k = 0; $k < $n; $k++) { $j = $k; $i = 0; while ($j >= 0) { $arr[$i][$j] = $p; $p++; $i = $i + 1; $j = $j - 1; } } // Store the values for lower // triangle of the pattern for ($k = 1; $k < $n; $k++) { $i = $k; $j = $n - 1; $f = $k; while ($j >= $f) { $arr[$i][$j] = $p; $p++; $i = $i + 1; $j = $j - 1; } } // Print the pattern for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { echo($arr[$i][$j] . " "); } echo("\n"); } } // Driver code $n = 3; printPattern($n); // This code is contributed // by Mukul Singh?> |
Javascript
<script> // Javascript program to print the required pattern // Function to print the required pattern function printPattern(n) { // arr[][] will store the pattern matrix let arr = new Array(n); for(let i = 0; i < n; i++) { arr[i] = new Array(n); for(let j = 0; j < n; j++) { arr[i][j] = 0; } } let k, i, j, p = 1, f ; // Store the values for upper triangle // of the pattern for (k = 0; k < n; k++) { j = k; i = 0; while (j >= 0) { arr[i][j] = p; p++; i = i + 1; j = j - 1; } } // Store the values for lower triangle // of the pattern for (k = 1; k < n; k++) { i = k; j = n - 1; f = k; while (j >= f) { arr[i][j] = p; p++; i = i + 1; j = j - 1; } } // Print the pattern for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { document.write(arr[i][j] + " ") ; } document.write("<br>") ; } } let n = 3; printPattern(n); // This code is contributed by decode2207.</script> |
1 2 4 3 5 7 6 8 9
Time complexity: O(n^2) for given n*n matrix
Auxiliary space: O(n^2) because using space for array arr
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More Information here on that Topic: geeksforgeeks.org/print-numbers-in-matrix-diagonal-pattern/ […]