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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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--;
        }
  
        // Conatsant 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) + 1): 
          
        m =
  
        # Decreasing part 
        for j in range(0, i): 
            print(m ,end= " " )
            m-=1
          
        # Conatsant 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 =
        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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for printing 
// the rectangular pattern 
  
// Function to print the pattern 
function printPattern($n
    // number of rows and columns 
    // to be printed 
    $s = 2 * $n - 1; 
  
    // Upper Half 
    for ($i = 0; 
         $i < (int)($s / 2) + 1; $i++)
    
          
        $m = $n
  
        // Decreasing part 
        for ($j = 0; $j < $i; $j++)
        
            echo $m , " "
            $m--; 
        
  
        // Conatsant Part 
        for ($k = 0; $k < $s - 2 * $i; $k++) 
        
            echo ($n - $i) , " "
        
  
        // Increasing part. 
        $m = $n - $i + 1; 
        for ($l = 0; $l < $i; $l++) 
        
            echo $m , " "
            $m++; 
        
  
        echo "\n"
    
  
    // Lower Half 
    for ($i = (int)($s / 2 - 1); 
         $i >= 0; $i--) 
    
  
        // Decreasing Part 
        $m = $n
        for ($j = 0; $j < $i; $j++)
        
            echo $m, " "
            $m--; 
        
  
        // Constant Part. 
        for ($k = 0; $k < $s - 2 * $i; $k++) 
        
            echo $n - $i , " "
        
  
        // Decreasing Part 
        $m = $n - $i + 1; 
        for ($l = 0; $l < $i; $l++)
        
            echo $m , " "
            $m++; 
        
        echo "\n"
    
  
// Driver Code
$n = 3; 
printPattern($n); 
  
// This code is contributed 
// by Sach_Code    
?>

chevron_right



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  

Another Approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for printing
// the rectangular pattern
  
// Function to print the pattern
function printPattern($n)
{
    $arraySize = $n * 2 - 1;
    $result=array_fill(0,$arraySize,array_fill(0,$arraySize,0));
          
    // Fill the values
    for($i = 0; $i < $arraySize; $i++)
    {
        for($j = 0; $j < $arraySize; $j++)
        {
            if(abs($i - (int)($arraySize / 2)) > 
                    abs($j - (int)($arraySize / 2)))
                $result[$i][$j] = abs($i
                        (int)($arraySize / 2)) + 1;
            else
                $result[$i][$j] = (abs($j-(int)
                                ($arraySize / 2)) + 1);
              
        }
    }
          
    // Print the array
    for($i = 0; $i < $arraySize; $i++)
    {
        for($j = 0; $j < $arraySize; $j++)
        {
            echo $result[$i][$j]." ";
        }
        echo "\n";
    }
}
  
// Driver Code
  
    $n = 3;
  
    printPattern($n);
  
// This code is contributed by mits
?>

chevron_right



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  


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.