Open In App

Print rectangular pattern with given center

Last Updated : 23 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 5
Output:
2 2 2 2 2  
2 1 1 1 2  
2 1 0 1 2  
2 1 1 1 2  
2 2 2 2 2 

Input: c1 = 3, c2 = 4, n = 7 
Output:
4 3 3 3 3 3 3 
4 3 2 2 2 2 2 
4 3 2 1 1 1 2 
4 3 2 1 0 1 2 
4 3 2 1 1 1 2 
4 3 2 2 2 2 2 
4 3 3 3 3 3 3 

 

Approach: This problem can be solved by using two nested loops. Follow the steps below to solve this problem:

  • Iterate in the range[0, N-1], using a variable i and do the following steps:
    • Iterate in the range[0, N-1], using a variable j and do the following steps:
      • Print maximum of abs(c1 – i) and abs(c2 – j).
    • Print new line.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the matrix filled
// with rectangle pattern having center
// coordinates are c1, c2
 
void printRectPattern(int c1, int c2, int n)
{
 
    // Iterate in the range[0, n-1]
    for (int i = 0; i < n; i++) {
        // Iterate in the range[0, n-1]
        for (int j = 0; j < n; j++) {
            cout << (max(abs(c1 - i), abs(c2 - j))) << " ";
        }
        cout << endl;
    }
}
// Driver Code
 
int main()
{
 
    // Given Input
    int c1 = 2;
    int c2 = 2;
    int n = 5;
 
    // Function Call
    printRectPattern(c1, c2, n);
    // This code is contributed by Potta Lokesh
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to print the matrix filled
// with rectangle pattern having center
// coordinates are c1, c2
static void printRectPattern(int c1, int c2, int n)
{
     
    // Iterate in the range[0, n-1]
    for(int i = 0; i < n; i++)
    {
         
        // Iterate in the range[0, n-1]
        for(int j = 0; j < n; j++)
        {
            System.out.print((Math.max(Math.abs(c1 - i),
                              Math.abs(c2 - j))) + " ");
        }
        System.out.println();
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    int c1 = 2;
    int c2 = 2;
    int n = 5;
     
    // Function Call
    printRectPattern(c1, c2, n);
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python3 program for the above approach
 
# Function to print the matrix filled
# with rectangle pattern having center
# coordinates are c1, c2
 
 
def printRectPattern(c1, c2, n):
 
    # Iterate in the range[0, n-1]
    for i in range(n):
        # Iterate in the range[0, n-1]
        for j in range(n):
            print(max(abs(c1 - i), abs(c2 - j)), end = " ")
        print("")
 
 
# Driver Code
 
# Given Input
c1 = 2
c2 = 2
n = 5
 
# Function Call
printRectPattern(c1, c2, n)


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to print the matrix filled
// with rectangle pattern having center
// coordinates are c1, c2
static void printRectPattern(int c1, int c2, int n)
{
     
    // Iterate in the range[0, n-1]
    for(int i = 0; i < n; i++)
    {
         
        // Iterate in the range[0, n-1]
        for(int j = 0; j < n; j++)
        {
            Console.Write((Math.Max(Math.Abs(c1 - i),
                           Math.Abs(c2 - j))) + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    int c1 = 2;
    int c2 = 2;
    int n = 5;
     
    // Function Call
    printRectPattern(c1, c2, n);
}
}
 
// This code is contributed by target_2


Javascript




<script>
// Javascript program for the above approach
 
// Function to print the matrix filled
// with rectangle pattern having center
// coordinates are c1, c2
function printRectPattern(c1, c2, n) {
 
    // Iterate in the range[0, n-1]
    for (let i = 0; i < n; i++)
    {
     
        // Iterate in the range[0, n-1]
        for (let j = 0; j < n; j++) {
            document.write(Math.max(Math.abs(c1 - i), Math.abs(c2 - j)) + " ");
        }
        document.write("<br>");
    }
}
 
// Driver Code
 
// Given Input
let c1 = 2;
let c2 = 2;
let n = 5;
 
// Function Call
printRectPattern(c1, c2, n);
 
    // This code is contributed by gfgking
</script>


Output: 

2 2 2 2 2 
2 1 1 1 2 
2 1 0 1 2 
2 1 1 1 2 
2 2 2 2 2

 

Time Complexity: O(N ^2)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads