Print this pattern! | Practice | GeeksforGeeks

Given an integer N, you need to print the following pattern.
Example 1:
Input:
N = 3
Output:
33333
32223
32123
32223
33333

Explanation:
When N = 3 then there will be three boundaries.
Outer boundary contains only 3.
Middle boundary contains only 2.
And the inner boundary contains only 1.
Example 2:
Input:
N = 4
Output:
4444444
4333334
4322234
4321234
4322234
4333334
4444444
Your Task:  
You don't need to read input. Your task is to complete the function printPattern() which takes an integer N as an input parameter and print the pattern for it.
Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
0
sahibealam295 months ago
 public:   void printPattern(int N)   {       int k=0;       for(int i=0;i<=(2*N-1)/2;i++){           k=0;           for(int j=0;j<2*N-1;j++){               cout<<N-k;               if(k+1<=i && j<N-1)                   k++;               if(j>=N-1 && k-1>=0 && j>=(2*N-2)-i)                   k--;           }           cout<<"\n";       }       for(int i=(2*N-1)/2-1;i>=0;i--){           k=0;           for(int j=0;j<2*N-1;j++){                cout<<N-k;               if(k+1<=i && j<N-1)                   k++;               if(j>=N-1 && k-1>=0 && j>=(2*N-2)-i)                   k--;           }           cout<<"\n";       }
-1
sahibealam295 months ago
 cout<<N-k;               if(k+1<=i && j<N-1)                   k++;               if(j>=N-1 && k-1>=0 && j>=(2*N-2)-i)                   k--;
+1
shashwatsatna5 months ago
        for(int i=1;i<=(2*n)-1;i++){
            for(int j=1;j<=(2*n)-1;j++){
                cout<<max(abs(n-i),abs(n-j))+1;
            }
            cout<<endl;
        }
0
ankitsinha1112006 months ago
class Solution
{
public:
    void printPattern(int N)
    {
        string k;
        int i,j;
        vector<string> a;
        for(i=0;i<2*N-1;i++)
        k+=N+'0';
        a.push_back(k);
        cout<<k<<endl;
        if(N==1)
        return;
        for(i=1;i<N;i++)
        {
            for(j=i;j<=k.size()-i-1;j++)
                k[j]=(N-i)+'0';
            cout<<k<<endl;
            if(i!=N-1)
            a.push_back(k);
        }
        for(i=a.size()-1;i>=0;i--)
        cout<<a[i]<<endl;
    }
};
0
rajesh02gfg6 months ago
Idk about abs() .. so I observe the pattern and limit of N is not too much its 9 so I've done this... worked with 0.2 time
void printPattern(int n)   {       // Write Your Code here        int m;   m=n*2-1;   for(int i=0; i<m; i++)   {       for (int j = 0; j < m; j++)       {           /* code */           if(i==0||j==0||i==m-1||j==m-1) cout<<n;           else if(i==1||j==1||i==m-2||j==m-2) cout<<n-1;           else if(i==2||j==2||i==m-3||j==m-3) cout<<n-2;           else if(i==3||j==3||i==m-4||j==m-4) cout<<n-3;           else if(i==4||j==4||i==m-5||j==m-5) cout<<n-4;           else if(i==5||j==5||i==m-6||j==m-6) cout<<n-5;           else if(i==6||j==6||i==m-7||j==m-7) cout<<n-6;           else if(i==7||j==7||i==m-8||j==m-8) cout<<n-7;           else if(i==8||j==8||i==m-9||j==m-9) cout<<n-8;       }       cout<<endl;   }      }
0
sayanhalderofficial6 months ago
class Solution{public:   void printPattern(int N)   {       // Write Your Code here       int R=(2*N)-1;       for(int i=1;i<R+1;i++)       {           for(int j=1;j<R+1;j++)           {               int m = std::max(abs(N-i),abs(N-j));               cout<<m+1;           }           cout<<endl;       };   }};
0
sanketsupekar7 months ago
Java Solution 0.3 Sec
class Solution{
    void printPattern(int N){
        // code here

        int n=(N*2)-1;
        int arr[]=new int[n];
        Arrays.fill(arr,N);
        print(arr);
        for(int i=1;i<N;i++)
        {
            int l=i,r=(n-i)-1;
            while(l<=r)
            {
                if(l==r)
                {
                    arr[l++]-=1;
                    break;
                }
                arr[l++]-=1;
                arr[r--]-=1;
            }
            print(arr);
        }
        
        for(int i=N-1;i>0;i--)
        {
            int l=i,r=(n-i)-1;
            while(l<=r)
            {
                if(l==r)
                {
                    arr[l++]+=1;
                    break;
                }
                arr[l++]+=1;
                arr[r--]+=1;
            }
            print(arr);
        }
       
    }
    void print(int []arr)
    {
        for(int i:arr)
        System.out.print(i);
        System.out.println();
    }
}
0
arushi4748 months ago
The concept of Spiral traversal in a 2D array can also be used to solve the problem, below is the implementation of the idea:
 void printPattern(int N)   {      int size=(2*N)-1;      int temp=N;      int arr[size][size];      int left=0,up=0,down=size-1,right=size-1;      int dir=1;      while(temp!=0)      {          switch(dir)          {              case 1:               {                  for(int i=left;i<=right;i++)                  {                      arr[up][i]=temp;                  }                  dir++;                  up++;              }              case 2:               {                  for(int i=up;i<=down;i++)                  {                      arr[i][right]=temp;                  }                  dir++;                  right--;              }              case 3:               {                  for(int i=right;i>=left;i--)                  {                      arr[down][i]=temp;                  }                  dir++;                  down--;              }              case 4:               {                  for(int i=down;i>=up;i--)                  {                      arr[i][left]=temp;                  }                  dir++;                  left++;              }              default:              {                  dir=1;                  temp--;                  break;              }          }      }      for(int i=0;i<size;i++)      {          for(int j=0;j<size;j++)          {              cout<<arr[i][j];          }          cout<<"\n";      }      // Write Your Code here   }
 
+1
badgujarsachin838 months ago
void printPattern(int N)
    {
        // Write Your Code here
       int no=2*N-1;
       for(int i=1;i<=no;i++){
           for(int j=1;j<=no;j++){
               int dis=max(abs(N-i),abs(N-j));
               cout<<dis+1;
           }
           cout<<endl;
       }
    }
-2
paulsabu9 months ago
class Solution
{
public:
    void printPattern(int N)
    {
        int val= (N*2)-1;
        for(int i=0;i<val;i++){
            for(int j=0;j<val;j++){
                if(i==0||j==0||i==val-1||j==val-1){
                    cout<<N;
                }
                else if(i==1||j==1||i==val-2||j==val-2){
                    cout<<N-1;
                }
                 else if(i==2||j==2||i==val-3||j==val-3){
                    cout<<N-2;
                }
                 else if(i==3||j==3||i==val-4||j==val-4){
                    cout<<N-3;
                }
                  else if(i==4||j==4||i==val-5||j==val-5){
                    cout<<N-4;
                }
                  else if(i==5||j==5||i==val-6||j==val-6){
                    cout<<N-5;
                }
                  else if(i==6||j==6||i==val-7||j==val-7){
                    cout<<N-6;
                }
                  else if(i==7||j==7||i==val-8||j==val-8){
                    cout<<N-7;
                }
                  else if(i==8||j==8||i==val-9||j==val-9){
                    cout<<N-8;
                }
            }
            cout<<endl;
        }
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.