Print concentric rectangular pattern in a 2d matrix (original) (raw)
Last Updated : 21 Dec, 2022
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 4 4 4 4 4 4 4 4
Input : n = 3 Output : 3 3 3 3 3 3 2 2 2 3 3 2 1 2 3 3 2 2 2 3 3 3 3 3 3
For the given n, the number of rows or columns to be printed will be 2*n - 1. We will print the matrix in two parts. We will first print upper half from rows from 0 to floor((2*n - 1)/2) and then second half from floor((2*n - 1)/2) + 1 to 2*n - 2.
Now for each row, we will print it in three parts. First part is decreasing sequence which will start from n and decrease by 1 in each iteration. The number of iteration will be equal to row number, the second part is a constant sequence where constant is n - i and it will be print 2*n - 1 - 2 * row number, and the third part is increasing sequence which is nothing but opposite of the first sequence.
For lower half, observe, it is a mirror image of upper half (excluding middle row). So, simply run a loop of the upper half from (2*n - 1)/2 to 0.
Below is the basic implementation of this approach:
C++ `
// C++ program for printing // the rectangular pattern #include <bits/stdc++.h> using namespace std;
// Function to print the pattern void printPattern(int n) { // number of rows and columns to be printed int s = 2 * n - 1;
// Upper Half
for (int i = 0; i < (s / 2) + 1; i++) {
int m = n;
// Decreasing part
for (int j = 0; j < i; j++) {
cout << m << " ";
m--;
}
// Constant Part
for (int k = 0; k < s - 2 * i; k++) {
cout << n - i << " ";
}
// Increasing part.
m = n - i + 1;
for (int l = 0; l < i; l++) {
cout << m << " ";
m++;
}
cout << endl;
}
// Lower Half
for (int i = s / 2 - 1; i >= 0; i--) {
// Decreasing Part
int m = n;
for (int j = 0; j < i; j++) {
cout << m << " ";
m--;
}
// Constant Part.
for (int k = 0; k < s - 2 * i; k++) {
cout << n - i << " ";
}
// Decreasing Part
m = n - i + 1;
for (int l = 0; l < i; l++) {
cout << m << " ";
m++;
}
cout << endl;
}
} // Driven Program int main() { int n = 3;
printPattern(n);
return 0;
}
Java
// Java program for printing // the rectangular pattern import java.io.*;
class GFG {
// Function to // print the pattern static void printPattern(int n) { // number of rows and // columns to be printed int s = 2 * n - 1;
// Upper Half
for (int i = 0;
i < (s / 2) + 1; i++)
{
int m = n;
// Decreasing part
for (int j = 0; j < i; j++)
{
System.out.print(m + " ");
m--;
}
// Constant Part
for (int k = 0;
k < s - 2 * i; k++)
{
System.out.print(n - i + " ");
}
// Increasing part.
m = n - i + 1;
for (int l = 0; l < i; l++)
{
System.out.print(m + " ");
m++;
}
System.out.println();
}
// Lower Half
for (int i = s / 2 - 1;
i >= 0; i--)
{
// Decreasing Part
int m = n;
for (int j = 0; j < i; j++)
{
System.out.print(m + " ");
m--;
}
// Constant Part.
for (int k = 0;
k < s - 2 * i; k++)
{
System.out.print(n - i + " ");
}
// Decreasing Part
m = n - i + 1;
for (int l = 0; l < i; l++)
{
System.out.print(m + " ");
m++;
}
System.out.println();
}
} // Driver Code public static void main (String[] args) { int n = 3;
printPattern(n);
} }
// This code is contributed // by anuj_67.
Python3
Python3 program for printing
the rectangular pattern
Function to print the pattern
def printPattern(n) :
# number of rows and columns to be printed
s = 2 * n - 1
# Upper Half
for i in range(0, int(s / 2)):
m = n
# Decreasing part
for j in range(0, i):
print(m ,end= " " )
m-=1
# Constant Part
for k in range(0, s - 2 * i):
print(n-i ,end= " " )
# Increasing part.
m = n - i + 1
for l in range(0, i):
print(m ,end= " " )
m+=1
print("")
# Lower Half
for i in range(int(s / 2),-1,-1):
# Decreasing Part
m = n
for j in range(0, i):
print(m ,end= " " )
m-=1
# Constant Part.
for k in range(0, s - 2 * i):
print(n-i ,end= " " )
# Decreasing Part
m = n - i + 1
for l in range(0, i):
print(m ,end= " " )
m+=1
print("")
Driven Program
if name=='main': n = 3 printPattern(n)
this code is contributed by Smitha Dinesh
Semwal
C#
// C# program for printing // the rectangular pattern using System;
class GFG {
// Function to // print the pattern static void printPattern(int n) { // number of rows and // columns to be printed int s = 2 * n - 1;
// Upper Half
for (int i = 0;
i < (s / 2) + 1; i++)
{
int m = n;
// Decreasing part
for (int j = 0; j < i; j++)
{
Console.Write(m + " ");
m--;
}
// Constant Part
for (int k = 0;
k < s - 2 * i; k++)
{
Console.Write(n - i + " ");
}
// Increasing part.
m = n - i + 1;
for (int l = 0; l < i; l++)
{
Console.Write(m + " ");
m++;
}
Console.WriteLine();
}
// Lower Half
for (int i = s / 2 - 1;
i >= 0; i--)
{
// Decreasing Part
int m = n;
for (int j = 0; j < i; j++)
{
Console.Write(m + " ");
m--;
}
// Constant Part.
for (int k = 0;
k < s - 2 * i; k++)
{
Console.Write(n - i + " ");
}
// Decreasing Part
m = n - i + 1;
for (int l = 0; l < i; l++)
{
Console.Write(m + " ");
m++;
}
Console.WriteLine();
}
} // Driver Code public static void Main () { int n = 3;
printPattern(n);
} }
// This code is contributed // by anuj_67.
PHP
JavaScript
`
Output
3 3 3 3 3 3 2 2 2 3 3 2 1 2 3 3 2 2 2 3 3 3 3 3 3
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(1)
Another Approach:
C++ `
// C++ program for printing // the rectangular pattern #include<bits/stdc++.h> using namespace std;
// Function to print the pattern void printPattern(int n) { int arraySize = n * 2 - 1; int result[arraySize][arraySize];
// Fill the values
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < arraySize; j++)
{
if(abs(i - arraySize / 2) > abs(j - arraySize / 2))
result[i][j] = abs(i - arraySize / 2) + 1;
else
result[i][j] = (abs(j-arraySize / 2) + 1);
}
}
// Print the array
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < arraySize; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
}
// Driver Code int main() { int n = 3;
printPattern(n);
return 0;
}
// This code is contributed // by Rajput-Ji.
Java
// Java program for printing // the rectangular pattern import java.io.*;
class GFG {
// Function to print the pattern static void printPattern(int n) { int arraySize = n * 2 - 1; int[][] result = new int[arraySize][arraySize];
//Fill the values
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < arraySize; j++)
{
result[i][j] = Math.max(Math.abs(i-arraySize/2),
Math.abs(j-arraySize/2))+1;
}
}
//Print the array
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < arraySize; j++)
{
System.out.print(result[i][j]);
}
System.out.println();
}
} // Driver Code public static void main (String[] args) { int n = 3;
printPattern(n);
} }
// This code is contributed // by MohitSharma23.
Python3
Python3 program for printing
the rectangular pattern
Function to print the pattern
def printPattern(n):
arraySize = n * 2 - 1;
result = [[0 for x in range(arraySize)]
for y in range(arraySize)];
# Fill the values
for i in range(arraySize):
for j in range(arraySize):
if(abs(i - (arraySize // 2)) >
abs(j - (arraySize // 2))):
result[i][j] = abs(i - (arraySize // 2)) + 1;
else:
result[i][j] = abs(j - (arraySize // 2)) + 1;
# Print the array
for i in range(arraySize):
for j in range(arraySize):
print(result[i][j], end = " ");
print("");
Driver Code
n = 3;
printPattern(n);
This code is contributed by mits
C#
// C# program for printing // the rectangular pattern using System;
class GFG {
// Function to print the pattern static void printPattern(int n) { int arraySize = n * 2 - 1; int[,] result = new int[arraySize,arraySize];
// Fill the values
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < arraySize; j++)
{
result[i,j] = Math.Max(Math.Abs(i-arraySize/2),
Math.Abs(j-arraySize/2))+1;
}
}
// Print the array
for(int i = 0; i < arraySize; i++)
{
for(int j = 0; j < arraySize; j++)
{
Console.Write(result[i,j]+" ");
}
Console.WriteLine();
}
}
// Driver Code public static void Main (String[] args) { int n = 3;
printPattern(n);
} }
// This code has been contributed by 29AjayKumar
PHP
JavaScript
`
Output
3 3 3 3 3 3 2 2 2 3 3 2 1 2 3 3 2 2 2 3 3 3 3 3 3
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(n2)
Similar Reads
- 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
- Pattern to print X in a rectangular box Given the value of length, print the X pattern in a box using # and " " Examples: Input : 10 Output : ########## ## ## # # # # # # # # # ## # # ## # # # # # # # # # ## ## ########## Input : 7 Output : ####### ## ## # # # # # # # # # # # ## ## ####### Below is the implementation to print X in a recta 4 min read
- 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 6 min read
- Print matrix in diagonal pattern Given a matrix of n*n size, the task is to print its elements in a diagonal pattern. Input : mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : 1 2 4 7 5 3 6 8 9. Explanation: Start from 1 Then from upward to downward diagonally i.e. 2 and 4 Then from downward to upward diagonally i.e 7, 5, 3 Th 14 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
- Print cells with same rectangular sums in a matrix Given a matrix of m x n matrix, we need to print all those cells at which sum of sub-matrix ended at this cell and sub-matrix starting at this cell is equal to remaining elements. For better understanding please see below diagram, Examples : Input : mat[][] = {1, 2, 3, 5, 4, 1, 0, 2, 0, 1, 2, 0, 7, 12 min read
- Print numbers in matrix diagonal pattern 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 patter 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
- 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
- Javascript Program to Print matrix in snake pattern Given 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, 5 2 min read