Magical Pattern


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 are the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the magical pattern
  
#include <iostream>
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));
}

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to print the magical pattern
#include <stdio.h>
  
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));
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)
  
# Prthe magical matrix
DisplayMatrix(matrix, (2 * N - 1))
  
# This code is contributed by 
# Mohit Kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

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


My Personal Notes arrow_drop_up


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.