Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Ninja And The Number Pattern

Easy
0/40
Average time to solve is 16m
profile
Contributed by
240 upvotes

Problem statement

Ninja has been given a task to print the required pattern and he asked for your help with the same.

You must print a matrix corresponding to the given number pattern.

Example:
Input: ‘N’ = 4

Output: 

4444444
4333334
4322234
4321234
4322234
4333334
4444444
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer ‘N’.
Output format:
Print a 2D array of integers that represent the number pattern.
Constraints :
1  <= N <= 10^2
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
33333
32223
32123
32223
33333
Sample Input 2 :
5
Sample Output 2 :
555555555
544444445
543333345
543222325
543212345
543222325
543333345
544444445
555555555
Sample Input 3 :
4
Sample Output 3 :
4444444
4333334
4322234
4321234
4322234
4333334
4444444
Hint

Can you iterate over the cells?

Approaches (1)
Brute Force

Approach: 

 

Here the pattern will be of size ‘K * K’ where ‘K = 2 * N - 1’ . First, we will find two integers ‘X = abs(i - N + 1)’ and ‘Y = abs(j - N + 1)’ it will tell us what number should be in the current cell of the pattern ‘(i, j)’.The cell should contain the number ‘max(X, Y) + 1’. Because we can see the mathematical pattern here every cell ‘(i, j)’ contains the number max of the above two values.
 

Algorithm: 

 

  • Initialize the variable ‘K = 2 * N - 1’
  • Iterate from 0 to ‘K’ with iterator ‘i’
    • Iterator from 0 to ‘K’ with iterator ‘j’
      • Initialize variable ‘X = abs(i - N + 1)’ and ‘Y = abs(j - N + 1)’
      • Print the value of ‘max(x, y) + 1’.
    • Print an empty line.
Time Complexity

O(N^2), Where N is the given input integer. 

 

Two nested loops are running (2*N - 1) * (2*N - 1) times so that time complexity would be the order of quadratic or N^2.

 

Hence the time complexity is O(N^2). 

Space Complexity

O(1)

 

Since we are only printing the pattern and not storing it, no extra space is used.

Code Solution
(100% EXP penalty)
Ninja And The Number Pattern
All tags
Sort by
Search icon

Interview problems

c++ code

void getNumberPattern(int n) {
    for (int i = 0; i < 2*n-1; i++) {
        for (int j = 0; j < 2*n-1; j++) {

            int minDist = min({i, j, 2*n-2-i, 2*n-2-j});
            cout << (n - minDist);
            
        }
        cout << endl;
    }
}
52 views
0 replies
0 upvotes

Interview problems

getNumberpattern

void getNumberPattern(int n) {

    // Write your code here.

    for(int i=0;i<2*n-1;i++){

        for(int j=0;j<2*n-1;j++){

            int top=i;

            int left=j;

            int right=(2*n-2)-j;

            int down=(2*n-2)-i;

            cout<<(n-min(min(top,down),min(left,right)));

        }

        cout<<endl;

    }

}

28 views
0 replies
0 upvotes

Interview problems

simple java code

public class Solution {

    public static void getNumberPattern(int n) {

        // Write your code here.

        int size = 2 * n - 1;

        int start = 0;

        int end = size - 1;

        int matrix[][] = new int[size][size];

        while (n != 0) {

            for (int i = start; i <= end; i++) {

                for (int j = start; j <= end; j++) {

                    if (i == start || i == end || j == start || j == end) {

                        matrix[i][j] = n;

                    }

                }

            }

            n--;

            start++;

            end--;

        }

        for (int i = 0; i < size; i++) {

            for (int j = 0; j < size; j++) {

                System.out.print(matrix[i][j]);

            }

            System.out.println();

        }

 

    }

}

86 views
0 replies
0 upvotes

Interview problems

Ninja and the number pattern

public class Solution {

    public static void getNumberPattern(int n) {

        // Write your code here.

        int X,Y;

        int k = 2*n-1;

        for(int i =0;i<k;i++)

        {

            for(int j=0;j<k;j++)

            {

                X=Math.abs(i-n+1);

                Y=Math.abs(j-n+1);

                System.out.print(Math.max(X,Y)+1);

            }

            System.out.println();

        }

    }

}

127 views
0 replies
0 upvotes

Interview problems

Sol using array

void getNumberPattern(int n) {

int size = 2*n-1;

    int start =0;

    int end = size-1;

    int arr[size][size];

    while(n!=0){

    for(int i=start; i<=end; i++)

    {

        for(int j=start; j<=end; j++)

        {

            if(i==start || i==end || j==start || j==end)

            {

                arr[i][j]=n;

            }

        }

    }

            start++;

            end--;

            n--;

    }

    for(int i=0; i<size; i++)

    {

        for(int j=0; j<size; j++)

        {

            cout << arr[i][j];

        }

        cout << endl;

    }

}

100 views
0 replies
2 upvotes

Interview problems

Ninja And The Number Pattern

void getNumberPattern(int n) {

  for(int i = 0; i<2*n-1; i++){

      for(int j = 0; j<2*n-1; j++){

          int top = i;

          int down = j;

          int left = (2*n-2)-j;

          int right = (2*n-2)-i;

 

          cout<<(n-min(min(top,down),min(left,right)));

      }

cout<<endl;

  }

}

104 views
0 replies
0 upvotes
profile

Interview problems

python sol using for loop

def getNumberPattern(n: int) -> None:

    # Write your solution here.

    for i in range(2*n-1):

        for j in range(2*n-1):

            top=i;left=j;right=(2*n-2)-j;down=(2*n-2)-i

            print(n-min(top,down,right,left),end="")

        print()

    pass

 

56 views
0 replies
1 upvote

Interview problems

Answer:

void getNumberPattern(int n) {

    for (int i=0;i<(n*2)-1;i++){

        for (int j=0;j<(n*2)-1;j++){

            int top=i;

            int left=j;

            int right=(2*n-2)-j;

            int down=(2*n-2)-i;

        cout<<n-min(min(top,down),min(right,left));

        

         

        }

        cout<<endl;

    }

}

 

66 views
0 replies
0 upvotes

Interview problems

best and short solution

for(int i=0;i<2*n-1;i++)

    {

        for(int j=0;j<2*n-1;j++)

        {

            int top=(2*n-2)-(i);

            int bottom=(2*n-2)-(j);

            cout<<(n-min(min(top,bottom),min(i,j)));

        }

        cout<<endl;

    }

}

94 views
0 replies
0 upvotes

Interview problems

Simple C++ Approach | Better than 98.5%

void getNumberPattern(int n) {

    for(int i = 0; i < 2*n-1; i++){

        cout << n;

    }

    cout << endl;

 

    for(int i = 0; i < n-1; i++){

        for(int j = 0; j <= i; j++)

            cout << n - j;

        for(int k = 0; k < 2*n-3 -2*i; k++)

            cout << n-i-1;

        for(int j = 0; j <= i; j++)

           cout << n-i+j;

        cout << endl;

    }

 

      for (int i = 0; i < n - 1; i++)

        {

            for(int j = 0; j < n-1-i; j++)

                cout << n-j;

            for(int k = 0; k < 2*i+1; k++)

                cout << 2+i;

            for(int j = 0; j < n-i-1; j++)

               cout << 2+j+i;

            cout << endl;

        }

    }

70 views
0 replies
0 upvotes
Full screen
Console