Print numbers in a concentric square pattern I am trying to solve a programming problem on a coding platform. When I execute it on my PC, it works perfectly, but when I submit the code on the coding platform, it throws a &quot;Time Limit Exceeded&quot; error. Can someone check my solution and help optimize it?


Given a number N, the program must print the pattern as described below.
Input Format:The first line contains the value of the N which represent the number N.
Output Format:The pattern as described below in the Example.

&gt; Input: 4

&gt; Output:
4444444
4333334
4322234
4321234
4322234
4333334   
4444444

My Approach
Print Upper rectangle and then print the lower rectangle.
The problem statement and my code can also be found here.

import java.util.Scanner;

public class Test004_3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        /*  PRINT UPPER PART
            4444444
            4333334
            4322234
            4321234
        */
        for(int i = N; i&gt;(1); i--)
        {
            for(int j = N; j &gt;i; j--)
                System.out.print(j);
            for(int j = 0; j&lt;(i*2)-1; j++)
               System.out.print(i);
            for(int j = i+1; j&lt;=N; j++)
                System.out.print(j);
            System.out.println(&quot;&quot;);
        }
        /* 
            PRINT LOWER PART
            4322234
            4333334
            4444444
        */
        for(int i = 1; i&lt;=(N); i++)
        {
            for(int j = N; j&gt;i; j--)
                System.out.print(j);
            for(int j = 0; j&lt;(i*2)-1; j++)
                System.out.print(i);
            for(int j = i+1; j &lt;=N; j++)
                System.out.print(j);
            System.out.println(&quot;&quot;);
        }
    }
}
As has been said by @200_success, outputting individual characters at a time to the console is going to be relatively slow.  You'd be better off building up the string using a StringBuilder and then writing the whole thing at the end.

Assuming a maximum number of 9, you can predict the size of the required buffer which will prevent the buffer from needing to resize.  A safe value would be:

StringBuilder sb = new StringBuilder(N*2*N*2)


This number will need to be bigger if you have to support multiple digit numbers, however the StringBuilder will scale this for you if required.

Instead of calling System.out.print, you then append to the StringBuilder:

sb.append(j);


Adding new lines when required:

sb.append("\n");


And at the end, write out the entire buffer in one go:

System.out.print(sb);


Putting it together

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    StringBuilder sb = new StringBuilder(N*2*N*2);
    for(int i = N; i&gt;(1); i--) //Print upper part
    {
        for(int j = N; j &gt;i; j--)
            sb.append(j);
        for(int j = 0; j&lt;(i*2)-1; j++)
           sb.append(i);
        for(int j = i+1; j&lt;=N; j++)
            sb.append(j);
        sb.append("\n");
    }
    for(int i = 1; i&lt;=(N); i++)// print lower part
    {
        for(int j = N; j&gt;i; j--)
            sb.append(j);
        for(int j = 0; j&lt;(i*2)-1; j++)
            sb.append(i);
        for(int j = i+1; j &lt;=N; j++)
            sb.append(j);
        sb.append("\n");
    }
    System.out.print(sb);
}