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

Number Crown

Easy
0/40
profile
Contributed by
130 upvotes

Problem statement

Aryan and his friends are very fond of the pattern. They want to make the Reverse N-Number Crown for a given integer' N'.

Given 'N', print the corresponding pattern.

Example:
Input: ‘N’ = 3

Output: 

1         1
1 2     2 1
1 2 3 3 2 1
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer 'N’.
Output format:
Print the pattern as specified in the problem statement.
Constraints :
1  <= N <= 20
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
1         1
1 2     2 1
1 2 3 3 2 1
Sample Input 2 :
4
Sample Output 2 :
1             1
1 2         2 1
1 2 3     3 2 1
1 2 3 4 4 3 2 1
Sample Input 3 :
7
Sample Output 3 :
1                         1
1 2                     2 1
1 2 3                 3 2 1
1 2 3 4             4 3 2 1
1 2 3 4 5         5 4 3 2 1
1 2 3 4 5 6     6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1  
Hint

Iterate to all the cells.

Approaches (1)
Brute Force

Approach: 

The solution is iterating on the pattern and printing every cell with the required number.

The steps are as follows :

Function void numberCrown(int ‘N’)

 

  1. Int ‘num’ = ‘1’, ‘gap’ = 2 * (’N’-1).
  2. For ‘i’ from 0 to ‘N’-1:
    • Initialise ‘currentNumber’ with ‘1’.
    • For j from 0 to ‘num’-1:
      • Print 'currentNumber'.
      • Increment ‘currentNumber’ by 1.
    • For j from ‘0’ to ’gap’-1
      • Print ' '.
    • Decrement ‘currentNumber’ by 1.
    • For j from ‘0’ to ‘num’ - 1:
      • Print 'currentNumber'.
      • Decrement ‘currentNumber’ by 1.
    • Increment ‘num’ by 1.
    • Decrement ‘gap’ by 2.
    • End the current line.
Time Complexity

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

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

Hence the time complexity is O( N * N ). 

Space Complexity

O(1).

Since we are using constant extra space.

Code Solution
(100% EXP penalty)
Number Crown
All tags
Sort by
Search icon

Interview problems

Using Java

public class Solution {

    public static void numberCrown(int n) {

        // Write your code here.

        int space= 2*(n-1);

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

        for(int j=1;j<=i;j++){

           System.out.print(j+" ");

        }

 

        for(int j=1;j<=space;j++){

           System.out.print(" ");

        }

        for(int j=i;j>=1;j--){

            System.out.print(j+" ");

        }

        System.out.println();

        space -=2;

    }

    }

}

22 views
0 replies
1 upvote

Interview problems

Using c++

void numberCrown(int n) {

    // Write your code here.

    int space= 2*(n-1);

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

        for(int j=1;j<=i;j++){

            cout<< j<<" ";

        }

 

        for(int j=1;j<=space;j++){

            cout<<" ";

        }

        for(int j=i;j>=1;j--){

            cout<< j<<" ";

        }

        cout<<endl;

        space -=2;

    }

}   

 

 

Step 

 

Outer loop for the number of rows.

for printing numbers in each row

for printing spaces in each row

for printing numbers in each row

As soon as the numbers for each iteration are printed, we move to the

next row and give a line break otherwise all numbers

would get printed in 1 line.

After each iteration nos. increases by 2, thus

spaces will decrease by 2.

19 views
0 replies
0 upvotes

Interview problems

Simple Python program

def numberCrown(n: int) -> None:
    # Write your solution here.
    for i in range(1, n+1):
        for j in range(1, i+1):
            print(j, end=' ')
        for space in range(1, 2*(n-i)):
            print(end=' ')
        for j in range(i, 0, -1):
            print(j, end=' ')
        print()
    pass

beginners

python

24 views
0 replies
0 upvotes

Interview problems

Easy Python Solution

   space=2*(n-1)

    for i in range(1,n+1):

        for j in range(1,i+1):

            print(j,end=" ")

        for j in range(1,space+1):

            print(" ",end=" ")

        for j in range(i,0,-1):

            print(j,end=" ")

        print()

13 views
0 replies
0 upvotes

Interview problems

Using Math & String

when we see into the problem , we can observe that  1. the input n

2. the number of rows , relate the input to the number of the rows. 3. then try to figure the number of columns , and figure a relation to the rows.

4. find if any pattern can be drawn between the rows and columns  

def numberCrown(n: int) -> None:
    for i in range(1,n+1):
        print(*range(1,i+1),end=" ")
        print(" "*2*((2*n)-(2*i+1)),end="")
        print(*range(i,0,-1),end=" ")
        print()
    pass
16 views
0 replies
0 upvotes

Interview problems

cried solving because it has 2 spaces not one

void numberCrown(int n) {

    // Write your code here.

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

 

        //number

        for(int j=1; j<=i; j++){

            cout<<j<<" ";

        }

 

        //space

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

            cout<<"  ";

        }

 

        //number

        for(int j=i; j>=1; j--){

            cout<<j<<" ";

        }

        

        cout<<endl;

    }

}

37 views
0 replies
0 upvotes

Interview problems

c++ code

void numberCrown(int n) {
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cout<<j<<" ";
        }
        for(int j=0;j<2*(n-i);j++ ){
            cout<<"  ";
        }
        for(int j=i;j>=1;j--){
            cout<<j<<" ";
        }
        cout<<endl;
    }
}
45 views
0 replies
0 upvotes

Interview problems

solution

void numberCrown(int n) {

    // Write your code here.

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

        for(int j=1;j<=i+1;j++){

            cout<<j<<" ";

        }

        for(int j=n;j>2*(i)-1;j--){

            cout<<" ";

        }

        for(int j=n;j>2*(i)-1;j--){

            cout<<" ";

        }

        for(int j=1;j<=i+1;j++){

            cout<<i-j+2<<" ";

        }

        cout<<endl;

    }

}

7 views
0 replies
1 upvote

Interview problems

for Java users

public class Solution {

    public static void numberCrown(int n) {

        // Write your code here.

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

            int j=0;

 

            //numbers

            for ( j = 1; j <= i; j++) {

                System.out.print(j+" ");

            }

 

            //space

            for (int s = n-i; s > 0; s--) {

                System.out.print(" ");

            }

 

            //space

            for (int s = n-i; s > 0; s--) {

                System.out.print(" ");

            }

 

            //numbers

            for (int k = j-1; k > 0; k--) {

                System.out.print(k+" ");

            }

 

            System.out.println();

  

        }

    }

}

28 views
1 reply
0 upvotes

Interview problems

CrownPattern

void numberCrown(int n) {

 // Write your code here.

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

        //number start

        for(int j=1;j<=i;j++){

            cout<< j<<" ";

        }

        //space

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

            cout<<"  ";

        }

        for(int j=i;j>=1;j--){

            cout<< j<<" ";

        }

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

            cout<<"  ";

        }

        cout<<endl;

    }

}

11 views
0 replies
0 upvotes
Full screen
Console