Related Articles

# Magical Pattern

• Difficulty Level : Expert
• Last Updated : 31 May, 2021

Given an integer N as input, the task is to print the Magical Pattern as given below:

N . . 3 2 1 2 3 . . N
. . . . . . . . . . .
3 3 3 3 2 1 2 3 3 3 3
2 2 2 2 2 1 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 1 2 2 2 2 2
3 3 3 3 2 1 2 3 3 3 3
. . . . . . . . . . .
N . . 3 2 1 2 3 . . N

Examples:

```Input: 3
Output:
3 2 1 2 3
2 2 1 2 2
1 1 1 1 1
2 2 1 2 2
3 2 1 2 3

Input: 4
Output:
4 3 2 1 2 3 4
3 3 2 1 2 3 3
2 2 2 1 2 2 2
1 1 1 1 1 1 1
2 2 2 1 2 2 2
3 3 2 1 2 3 3
4 3 2 1 2 3 4  ```

Approach:

1. Consider an input N = 3, so the overlapping matrix will be of row = 5 and column = 5 (ie 2 * N – 1) with all the entries as zero.
2. Define three variables start = N, inc = 0 and dec = 2 * N – 1 for manipulation.
3. Run a loop till start becomes zero.
4. The row with inc or (dec – 1) or the column with inc or (dec – 1) is filled with the start value.
so at start=3
matrix will be
3 3 3 3 3
3 0 0 0 3
3 0 0 0 3
3 0 0 0 3
3 3 3 3 3

5. Decrement dec, start, and increment the inc value.
6. Repeat the step3
7. The loop will stop as start=0 and the desired pattern is obtained

Below is the implementation of the above approach:

## C++

 `// C++ program to print the magical pattern` `#include ``using` `namespace` `std;` `void` `overLapping(``int` `N, ``int` `matrix[100][100])``{` `    ``int` `max, inc = 0, dec, start;` `    ``// The size of matrix``    ``max = 2 * N - 1;` `    ``// Fixing the range``    ``dec = max;` `    ``// Overlapping value``    ``start = N;` `    ``while` `(start != 0) {``        ``for` `(``int` `row = 0; row < max; row++) {``            ``for` `(``int` `col = 0; col < max; col++) {``                ``if` `(row == inc``                    ``|| row == dec - 1``                    ``|| col == dec - 1``                    ``|| col == inc) {` `                    ``matrix[row][col] = start;``                ``}``            ``}``        ``}``        ``start--;``        ``inc++;``        ``dec--;``    ``}` `    ``// return matrix;``}` `void` `DisplayMatrix(``int` `matrix[][100], ``int` `max)``{``    ``// To display the overlapping matrix``    ``for` `(``int` `row = 0; row < max; row++) {``        ``for` `(``int` `col = 0; col < max; col++) {``            ``cout <<``" "``<< matrix[row][col];``        ``}``        ``cout << endl;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `N;` `    ``// Get the value of N``    ``N = 3;` `    ``// Declaring the overlapping matrix``    ``int` `matrix[100][100];` `    ``// Create the magical matrix``    ``overLapping(N, matrix);` `    ``// Print the magical matrix``    ``DisplayMatrix(matrix, (2 * N - 1));``}`

## C

 `// C program to print the magical pattern``#include ` `void` `overLapping(``int` `N, ``int` `matrix[100][100])``{` `    ``int` `max, inc = 0, dec, start;` `    ``// The size of matrix``    ``max = 2 * N - 1;` `    ``// Fixing the range``    ``dec = max;` `    ``// Overlapping value``    ``start = N;` `    ``while` `(start != 0) {``        ``for` `(``int` `row = 0; row < max; row++) {``            ``for` `(``int` `col = 0; col < max; col++) {``                ``if` `(row == inc``                    ``|| row == dec - 1``                    ``|| col == dec - 1``                    ``|| col == inc) {` `                    ``matrix[row][col] = start;``                ``}``            ``}``        ``}``        ``start--;``        ``inc++;``        ``dec--;``    ``}` `    ``// return matrix;``}` `void` `DisplayMatrix(``int` `matrix[][100], ``int` `max)``{``    ``// To display the overlapping matrix``    ``for` `(``int` `row = 0; row < max; row++) {``        ``for` `(``int` `col = 0; col < max; col++) {``            ``printf``(``"%d "``, matrix[row][col]);``        ``}``        ``printf``(``"\n"``);``    ``}``}` `// Driver code``int` `main()``{``    ``int` `N = 3;` `    ``// Declaring the overlapping matrix``    ``int` `matrix[100][100];` `    ``// Create the magical matrix``    ``overLapping(N, matrix);` `    ``// Print the magical matrix``    ``DisplayMatrix(matrix, (2 * N - 1));``}`

## Java

 `// Java program to print the magical pattern` `class` `GFG {` `    ``static` `void` `overLapping(``int` `N, ``int` `matrix[][]) {` `        ``int` `max, inc = ``0``, dec, start;` `        ``// The size of matrix``        ``max = ``2` `* N - ``1``;` `        ``// Fixing the range``        ``dec = max;` `        ``// Overlapping value``        ``start = N;` `        ``while` `(start != ``0``) {``            ``for` `(``int` `row = ``0``; row < max; row++) {``                ``for` `(``int` `col = ``0``; col < max; col++) {``                    ``if` `(row == inc``                            ``|| row == dec - ``1``                            ``|| col == dec - ``1``                            ``|| col == inc) {` `                        ``matrix[row][col] = start;``                    ``}``                ``}``            ``}``            ``start--;``            ``inc++;``            ``dec--;``        ``}` `        ``// return matrix;``    ``}` `    ``static` `void` `DisplayMatrix(``int` `matrix[][], ``int` `max) {``        ``// To display the overlapping matrix``        ``for` `(``int` `row = ``0``; row < max; row++) {``            ``for` `(``int` `col = ``0``; col < max; col++) {``                ``System.out.printf(``"%d "``, matrix[row][col]);``            ``}``            ``System.out.printf(``"\n"``);``        ``}``    ``}` `// Driver code``    ``public` `static` `void` `main(String[] args) {``        ``int` `N = ``3``;` `        ``// Declaring the overlapping matrix``        ``int` `matrix[][] = ``new` `int``[``100``][``100``];` `        ``// Create the magical matrix``        ``overLapping(N, matrix);` `        ``// Print the magical matrix``        ``DisplayMatrix(matrix, (``2` `* N - ``1``));``    ``}``}` `// This code is contributed by Rajput-JI`

## Python3

 `# Python3 program to print the magical pattern` `def` `overLapping(N, matrix):` `    ``inc ``=` `0` `    ``# The size of matrix``    ``Max` `=` `2` `*` `N ``-` `1` `    ``# Fixing the range``    ``dec ``=` `Max` `    ``# Overlapping value``    ``start ``=` `N` `    ``while` `(start !``=` `0``):``        ``for` `row ``in` `range``(``Max``):``            ``for` `col ``in` `range``(``Max``):``                ``if` `(row ``=``=` `inc ``or` `row ``=``=` `dec ``-` `1` `or``                    ``col ``=``=` `dec ``-` `1` `or` `col ``=``=` `inc):``                    ``matrix[row][col] ``=` `start``            ` `        ``start ``-``=` `1``        ``inc ``+``=` `1``        ``dec ``-``=` `1``    ` `    ``# return matrix` `def` `DisplayMatrix(matrix, ``Max``):` `    ``# To display the overlapping matrix``    ``for` `row ``in` `range``(``Max``):``        ``for` `col ``in` `range``(``Max``):``            ``print``(matrix[row][col], end ``=` `" "``)``        ` `        ``print``()``    ` `# Driver code` `# Get the value of N``N ``=` `3` `# Declaring the overlapping matrix``matrix ``=` `[[``0` `for` `i ``in` `range``(``100``)]``             ``for` `i ``in` `range``(``100``)]` `# Create the magical matrix``overLapping(N, matrix)` `# Print the magical matrix``DisplayMatrix(matrix, (``2` `*` `N ``-` `1``))` `# This code is contributed by``# Mohit Kumar 29`

## C#

 `// C# program to print the magical pattern``using` `System;` `class` `GFG``{``public` `static` `void` `overLapping(``int` `N,``                               ``int``[,] matrix)``{``    ``int` `max, inc = 0, dec, start;``    ``max = 2 * N - 1;``    ``dec = max;``    ``start = N;` `    ``while` `(start != 0)``    ``{``        ``for` `(``int` `row = 0; row < max; row++)``        ``{``            ``for` `(``int` `col = 0; col < max; col++)``            ``{``                ``if` `(row == inc || row == dec - 1 ||``                    ``col == dec - 1 || col == inc)``                ``{``                    ``matrix[row, col] = start;``                ``}``            ``}``        ``}``        ``start--;``        ``inc++;``        ``dec--;``    ``}``}` `public` `static` `void` `DisplayMatrix(``int``[,] matrix, ``int` `max)``{``    ``for` `(``int` `row = 0; row < max; row++)``    ``{``        ``for` `(``int` `col = 0; col < max; col++)``        ``{``            ``Console.Write(``" {0}"``, matrix[row, col]);``        ``}``        ``Console.Write(``"\n"``);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `N;``    ``N = 3;` `    ``int``[,] matrix = ``new` `int``[100, 100];` `    ``overLapping(N, matrix);``    ``DisplayMatrix(matrix, (2 * N - 1));``}``}` `// This code is contributed by DrRoot_`

## PHP

 ``

## Javascript

 ``
Output:
```3 2 1 2 3
2 2 1 2 2
1 1 1 1 1
2 2 1 2 2
3 2 1 2 3```

Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students

My Personal Notes arrow_drop_up