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

Star Diamond

Easy
0/40
profile
Contributed by
120 upvotes

Problem statement

Ninja was very fond of patterns. For a given integer ‘N’, he wants to make the N-Star Diamond.

Example:
Input: ‘N’ = 3

Output: 

  *
 ***
*****
*****
 ***
  *
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line of each test case contains an integer ‘N’.
Output format:
Print the pattern as specified.
Constraints :
1  <= N <= 20
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
  *
 ***
*****
*****
 ***
  *    
Sample Input 2 :
1
Sample Output 2 :
*
*
Hint

Iterate to all the cells.

Approaches (1)
Brute Force

Approach: 

The solution is iterating on the pattern and print every cell using ‘*’ or spaces.

The steps are as follows :

Function void nDiamond(int ‘N’)

 

  1. Int ‘gap’ = ‘N’-1, ‘stars’ = 1.
  2. For ‘i’ from 0 to ‘N’-1:
    • For j from 0 to ‘gap’:
      • Print ’ ’
    • For j from gap+1 to gap+stars:
      • Print ’*’
    • gap–
    • star+=2
  3. ‘gap’ = 0, ‘stars’ = 2*’N’-1.
  4. For ‘i’ from N to 2*‘N’-1:
    • For j from 0 to ‘gap’:
      • Print ’ ’
    • For j from gap+1 to gap+stars:
      • Print ’*’
    • gap++
    • star-=2
Time Complexity

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

Two nested loops are running (2*N)*(2*N-1) 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)
Star Diamond
All tags
Sort by
Search icon

Interview problems

These the basic code of Python

def nStarDiamond(n: int) -> None:
    for  i in range(n):
        for space in range(n-i-1):
            print(end=' ')
        for j in range(2*i+1):
            print('*', end='')
        print()

    for i in range(n, 0, -1):
        for space in range(n-i):
            print(end=' ')
        for j in range(2*i-1):
            print('*', end='')
        print()

    pass
31 views
0 replies
0 upvotes

Interview problems

codz with JAVA

public class Solution {

    public static void nStarDiamond(int n) {

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

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

                System.out.print(" ");

            }

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

                System.out.print("*");

            }

            System.out.println();

        }

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

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

                System.out.print(" ");

            }

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

                System.out.print("*");    

            }

            System.out.println();

        }

    }

}

61 views
0 replies
0 upvotes

Interview problems

cpp

void nStarDiamond(int n) {

    // Write your code here.

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

        //space

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

            cout<<" ";

        }

        //star

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

            cout<<"*";

        }

        //space

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

            cout<<" ";

        }

        cout<<endl;

    }

 

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

        //space

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

            cout<<" ";

        }

        //star

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

            cout<<"*";

 

        }

        //space

 

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

            cout<<" ";

        }

        cout<<endl;

    }

}

56 views
0 replies
0 upvotes

Interview problems

solution

void nStarDiamond(int n) {

    

    // Write your code here.

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

        int x=n;

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

            cout<<" ";

            

        }

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

            cout<<"*";

 

        }

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

            cout << " ";

        }

        cout<<endl;

        

    }

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

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

            cout<<" ";

        }

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

            cout<<"*";

 

        }

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

            cout << " ";

        }

        cout<<endl;

    }

 

}

14 views
0 replies
2 upvotes

Interview problems

c++ code

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

Interview problems

nstarDiamond

void nStarDiamond(int n) {

    // Write your code here.

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

         //space

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

            cout<<" ";

        }

        //star

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

            cout<<"*";

        }

        //space

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

            cout<< " ";

        }

        cout<<endl;

    }

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

        //space

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

            cout<< " ";}

        //star

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

            cout<<"*";

        }

        //space

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

            cout<< " ";

        }

       cout<<endl;

    }

}

12 views
0 replies
0 upvotes

Interview problems

in c++

void nStarDiamond(int n) {

    // Write your code here.

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

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

            cout<< " ";

        }

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

            cout<< "*";

        }

        cout<<endl;

    }

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

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

            cout<< " ";

        }

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

            cout<< "*";

        }

        cout<< endl;

    }

}

31 views
0 replies
0 upvotes

Interview problems

Beginner Friendly Solution || C++

void nStarDiamond(int n) {

    // Write your code here.

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

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

            cout<<" ";

        }

 

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

            cout<<"*";

        }

 

        cout<<endl;

    }

  

 

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

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

            cout<<" ";

        }

 

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

            cout<<"*";

        }

 

        cout<<endl;

    }

 

}

26 views
0 replies
0 upvotes

Interview problems

Java code for diamond pattern

public class Solution {

    public static void nStarDiamond(int n) {

        // Write your code here

 

          //first half

 

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

             //space

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

 

                 System.out.print(" ");

            }

             //star

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

 

                

                System.out.print("*");

                

            }

            System.out.println();

         }

 

           //second half

 

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

             //space

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

 

                 System.out.print(" ");

            }

             //star

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

 

                

                System.out.print("*");

                

            }

            System.out.println();

         }

 

         

    }

}

46 views
0 replies
0 upvotes

Interview problems

Java Code

public static void nStarDiamond(int n) {
        int a = 0;
        int b = n;
        for(int i=0; i<=n*2; i++){
           for(int j=1; j<(n*2); j++){
               if(i<n){
                    if( j < n - a){
                        System.out.print(" ");
                    }else if(j <= n + a){
                        System.out.print("*");    
                    }   
               }else if(i>n){
                   if( j < n - b){
                        System.out.print(" ");
                    }else if(j <= n + b){
                        System.out.print("*");    
                    }
               }
                
            }
            if(i!=n){System.out.println();}
            a++;
            if(i>=n){
                b--;
            }
        }
    }
20 views
0 replies
0 upvotes
Full screen
Console