How to learn Pattern printing easily?

The task is to print a pattern as shown in the example for a given integer value.

The goal is not to print just this one pattern, it is to learn the best approach to solve this kind of problems as these questions are frequently asked in coding exams and in job interviews.

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 = 7
Output:
7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7

For the tutorial, an example of N = 4 is used.

  • Step 1: First of all, analyse the pattern for any lines of symmetry. Here our pattern is both vertically and horizontally symmetrical, so draw the lines of symmetry like this,

    After breaking the pattern in parts, first try to draw only the upper-left part, namely, part A. If there is not any line of symmetry, jump to Step 2.

  • Step 2: Now associate each cell i.e. element with a row and column no.( usually denoted by i and j, respectively) Just like,

    From now on, a cell is denoted by C(i, j) with its row and column no.

  • Step 3: In this step, try and find a relation between the value of C(i, j) with i and/or j. Usually, the value of C depends on the value of N and values of i and j relative to each other. To elaborate,

    – In 1st row, every element is same i.e. 4(=N). So it is not of much help.
    – In 2nd row, C can be seen decreasing from of 4 to 3 for i>=j and then it stays 3 for next i=j and then it stays 2 for next i=j for all values of j, C can be seen decreasing from 4 to 1.

    So in every row, C starts from N, decreases by 1 until i>=j, and then becomes constant. Here the value of C depends upon the smaller between i and j and the formula could be:

    C(i, j) = N - min(i, j) + 1
    

    So our approach should be:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java progarm to find triplets 
    // a[i]>a[j]>a[k] and i<j<k
    import java.util.*;
      
    class GFG
    {
      
    // Driver code
    public static void main(String[] args)
    {
        int N = 4, i, j, min;
        System.out.println("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
    }
    }
      
    // This code is contributed by Princi Singh

    chevron_right

    
    

    Python3

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python3 progarm to find triplets 
    # a[i]>a[j]>a[k] and i<j<k
    if __name__ == '__main__':
          
        N = 4;
        print("Value of N: ", N);
      
        for i in range(1, N + 1):
            for j in range(1, N + 1):
                min = i if i < j else j;
                print(N - min + 1, end = "");
            print();
      
    # This code is contributed 
    # by PrinciRaj1992 

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# progarm to find triplets 
    // a[i]>a[j]>a[k] and i<j<k
    using System;
          
    class GFG
    {
      
    // Driver code
    public static void Main(String[] args)
    {
        int N = 4, i, j, min;
        Console.WriteLine("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
    }
    }
      
    // This code is contributed by PrinciRaj1992

    chevron_right

    
    

    Output:

    Value of N: 4
    4444
    4333
    4322
    4321
    

    If the pattern didn’t have any symmetry and is now completed, the work is done. But for patterns with symmetry, they are still incomplete and it is required to go to step 4.

  • Step 4: Now include part B of the pattern in the picture and associate those elements with column numbers. But don’t assign column numbers serial-wise, instead, assign them the same column number that was assigned to its mirror column in part A.

  • Step 5: Now change the code to append part B. To do that, it is just needed to rerun the inner loop for j = n-1 to j=1.

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class GFG 
    {
        public static void main(String[] args) 
        {
            int N = 4, i, j, min;
            System.out.println("Value of N: " + N);
          
            for (i = 1; i <= N; i++) 
            {
                for (j = 1; j <= N; j++) 
                {
                    min = i < j ? i : j;
                    System.out.print(N - min + 1);
                }
                for (j = N - 1; j >= 1; j--)
                {
                    min = i < j ? i : j;
                    System.out.print(N - min + 1);
                }
                System.out.println();
            }
        }
    }
      
    // This code is contributed by PrinciRaj1992

    chevron_right

    
    

    Python3

    N = 4;
    print(“Value of N: “, N);

    for i in range(1, N + 1):
    for j in range(1, N + 1):
    min = i if i < j else j; print(N - min + 1, end = ""); for j in range(N - 1, 0, -1): min = i if i < j else j; print(N - min + 1, end = ""); print(); # This code is contributed by Rajput-Ji [tabby title="C#"]

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    using System;
      
    class GFG 
    {
        public static void Main(String[] args) 
        {
            int N = 4, i, j, min;
            Console.WriteLine("Value of N: " + N);
          
            for (i = 1; i <= N; i++) 
            {
                for (j = 1; j <= N; j++) 
                {
                    min = i < j ? i : j;
                    Console.Write(N - min + 1);
                }
                for (j = N - 1; j >= 1; j--)
                {
                    min = i < j ? i : j;
                    Console.Write(N - min + 1);
                }
                Console.WriteLine();
            }
        }
    }
      
    // This code is contributed by Rajput-Ji

    chevron_right

    
    

    Output:

    Value of N: 4
    4444444
    4333334
    4322234
    4321234
    
  • Step 6: Now includes part C and D of the pattern in the picture and associate those elements with row numbers in the same way the column numbers are assigned in step 4.

  • Step 7: Now, In the same way, as done in step 5, change the code to append part C and D. To do that, one just need to rerun the outer loop for i = n-1 to i=1.

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
        int N = 4, i, j, min;
        cout << "Value of N: " << N << endl;
      
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        for (i = N - 1; i >= 1; i--) {
            for (j = 1; j <= N; j++) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            for (j = N - 1; j >= 1; j--) {
                min = i < j ? i : j;
                cout << N - min + 1;
            }
            cout << endl;
        }
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java implementation of the approach
    class GFG 
    {
    public static void main(String[] args) 
    {
        int N = 4, i, j, min;
        System.out.println("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++)
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--)
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
          
        for (i = N - 1; i >= 1; i--) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--) 
            {
                min = i < j ? i : j;
                System.out.print(N - min + 1);
            }
            System.out.println();
        }
        }
    }
      
    // This code is contributed by 29AjayKumar

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# implementation of the approach
    using System;
      
    class GFG 
    {
    public static void Main(String[] args) 
    {
        int N = 4, i, j, min;
        Console.WriteLine("Value of N: " + N);
      
        for (i = 1; i <= N; i++) 
        {
            for (j = 1; j <= N; j++)
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--)
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
          
        for (i = N - 1; i >= 1; i--) 
        {
            for (j = 1; j <= N; j++) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            for (j = N - 1; j >= 1; j--) 
            {
                min = i < j ? i : j;
                Console.Write(N - min + 1);
            }
            Console.WriteLine();
        }
        }
    }
      
    // This code is contributed by Rajput-Ji

    chevron_right

    
    

    Output:

    Value of N: 4
    4444444
    4333334
    4322234
    4321234
    4322234
    4333334
    4444444
    


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.