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;
}
}Problem of the day
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:
****
* *
* *
****
The first line contains an integer ‘N’.
Output format:
Print a 2D array of characters that represent the star pattern.
1 <= N <= 10^2
Time Limit: 1 sec
3
***
* *
***
5
*****
* *
* *
* *
*****
8
********
* *
* *
* *
* *
* *
* *
********
Iterate to all the cells and fill the first and last row separately and then fill the middle rows.
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:
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 ).
O(1).
No extra space is used as we are just printing the pattern.
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;
}
}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;
}
}
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
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();
}
}
}
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";
}
}
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;
}
}
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;
}
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;
}
}
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();
}
}
}
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;
}
}