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

Ninja And The Star Pattern I

Easy
0/40
Average time to solve is 13m
profile
Contributed by
60 upvotes

Problem statement

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

You must return an ‘N’*’N’ matrix corresponding to the given star pattern.

Example:
Input: ‘N’ = 4

Output: 

****
*  *
*  *
****
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer ‘N’.
Output format:
Print a 2D array of characters that represent the star pattern.
Constraints :
1  <= N <= 10^2
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
***
* *
***
Sample Input 2 :
5
Sample Output 2 :
*****
*   *
*   *
*   *
*****
Sample Input 3 :
8
Sample Output 3 :
********
*      *
*      *
*      *
*      *
*      *
*      *
********
Hint

Iterate to all the cells and fill the first and last row separately and then fill the middle rows.

Approaches (1)
Brute Force

Approach: 

 

If we see the pattern first and last rows are fully filled and the middle ‘N - 2’ rows are partially filled. We will iterate over the cells and fill the first and last rows fully and the middle rows will be filled with stars only if the cell is on edge.

 

Algorithm:

 

  1. For ‘i’ from 0 to ‘N’-1:
    • For j from 0 to ‘N-1’:
      • If ‘i == 0’ or ‘j == 0 or ‘i == N-1’ or ‘j == N-1’:
        • pattern (i)(j) = ‘*’
      • Else  :
        • pattern (i)(j) = ‘ ’
  2. Print the ‘pattern’.
Time Complexity

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

 

Two nested loops are running N * 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).

No extra space is used as we are just printing the pattern.

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

Interview problems

c++ code

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

Interview problems

getStarpattern

void getStarPattern(int n) {

    // Write your code here.

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

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

            if(i==0 || j==0 || j==n-1 || i==n-1){

                cout<<"*";

            } 

            else {

                cout<<" ";

            }

        }

        cout<<endl;

    }

}

 

7 views
0 replies
0 upvotes

Interview problems

Observe the Symmetry || Easy || Direct || Python

def getStarPattern(n: int) -> None:
    for i in range(1,n+1):
        if i == 1 or i == n:
            print("*"*n,end="")
        else:
            print("*",end="")
            print(" "*(n-2),end="")
            print("*",end="")
        print()
    pass
14 views
0 replies
1 upvote

Interview problems

java Codz

public class Solution {

    public static void getStarPattern(int n) {

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

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

                if(i == 1 ||i == n || j == 1 || j == n){

                    System.out.print("*");

                }

                else{

                    System.out.print(" ");

                }

            }

            System.out.println();

        }

    }

}

22 views
0 replies
0 upvotes

Interview problems

c++

void getStarPattern(int n) {

    // Write your code here.

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

    {

        if(i==0 || i==n-1)

        {

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

                cout<<"*";

        }else{

            cout<<"*";

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

                cout<<" ";

            cout<<"*";

        }

 

        cout<<"\n";

    }

}

 

21 views
0 replies
0 upvotes

Interview problems

Ninja And The Star Pattern I

void getStarPattern(int n) {

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

  {

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

      {

          if(i==0||i==n-1||j==0||j==n-1)

          {

              cout<<"*";

          }

          else cout<<" ";

      }

      cout<<endl;

  }

}

 

27 views
0 replies
0 upvotes

Interview problems

using Strings CPP

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

{

if (i == 0 || i == n - 1)

{

// Print the top and bottom rows

cout << string(n, '*');

}

else

{

// Print the middle rows with spaces between the borders

cout << "*" << string(n - 2, ' ') << "*";

}

cout << endl;

}

 

 

20 views
0 replies
0 upvotes

Interview problems

EASY C++ SOLUTION

void getStarPattern(int n) {

    // Write your code here.

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

    {

         if(i==0||i==n-1)

         {

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

             {

                 cout<<"*";

             }

             cout<<endl;

             continue;

         }

         cout<<"*";

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

         {

             cout<<" ";

         }

         cout<<"*";

         cout<<endl;

    }

}

 

24 views
0 replies
0 upvotes

Interview problems

Hollow Rectangle Pattern

import java.util.*;

class Solution {    public static void getStarPattern(int n){

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

        {    

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

            {

                if((i==0||i==n-1)||(j==0||j==n-1))

                {

                    System.out.print("*");

                }

                else

                {

                    System.out.print(" ");

                }

            }

            System.out.println();

        }

    }

}

31 views
0 replies
0 upvotes

Interview problems

Ninja And The Star Pattern I

void getStarPattern(int n) {

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

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

           if(i==0|| j==0|| i==n-1 || j==n-1){

               cout<<"*";

           }

           else cout<<" ";

       }

       cout<<endl;

   }

}

 

18 views
0 replies
0 upvotes
Full screen
Console