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 2Input: 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.
- Iterate in the range[0, N-1], using a variable j and do the following steps:
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, c2void 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 Codeint 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 approachimport java.io.*;class GFG{// Function to print the matrix filled// with rectangle pattern having center// coordinates are c1, c2static 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 codepublic 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, c2def 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 Inputc1 = 2c2 = 2n = 5# Function CallprintRectPattern(c1, c2, n) |
C#
// C# program for the above approachusing System;class GFG{// Function to print the matrix filled// with rectangle pattern having center// coordinates are c1, c2static 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 Codepublic 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, c2function 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 Inputlet c1 = 2;let c2 = 2;let n = 5;// Function CallprintRectPattern(c1, c2, n); // This code is contributed by gfgking</script> |
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More Info here on that Topic: geeksforgeeks.org/print-rectangular-pattern-with-given-center/ […]