for(int i=0;i<=2*n-1;i++)
{
int stars=i;
if(i>n) stars=2*n-i;
for(int j=0;j<stars;j++)
System.out.print("*");
System.out.println();
}
Problem of the day
Ninja was very fond of patterns. For a given integer ‘N’, he wants to make the N-Star Rotated Triangle.
Example:Input: ‘N’ = 3
Output:
*
**
***
**
*
The first and only line of each test case contains an integer ‘N’.
Output format:
Print the pattern as specified.
1 <= N <= 20
Time Limit: 1 sec
3
*
**
***
**
*
1
*
Iterate to all the cells.
Approach:
The solution is iterating on the pattern and printing every cell using ‘*’ or spaces.
The steps are as follows :
Function void nStarTriangle(int ‘N’)
O( N * N ), Where N is the given input integer.
Two nested loops are running ( ( 2 * N) - 1) * N times so that time complexity would be the order of N * N.
Hence the time complexity is O( N * N ).
O(1)
Since we are using constant extra space, the space complexity is of the order O(1).
Interview problems
my approach
for(int i=0;i<=2*n-1;i++)
{
int stars=i;
if(i>n) stars=2*n-i;
for(int j=0;j<stars;j++)
System.out.print("*");
System.out.println();
}
Interview problems
CPP Code
void nStarTriangle(int n)
{
for(int i=1;i<=2*n-1;i++)
{
if(i<=n)
{
for (int j = 1; j <= i; j++)
{
cout << "*";
}
cout<<endl;
}
else
{
for(int j=1; j <= 2*n-i; j++)
{
cout<<"*";
}
cout<<endl;
}
}
}
Interview problems
simple Python code for staters
def nStarTriangle(n: int) -> None:
# Write your code here.
for i in range(n):
for j in range(i+1):
print('*', end='')
print()
for i in range(n-1, 0, -1):
for j in range(i):
print('*', end='')
print()
passInterview problems
cpp easy
void nStarTriangle(int n) {
// Write your code here.
for(int i=1; i<=(2*n - 1); i++){
int stars = i;
if (i > n) {
stars = (2 * n - i);
};
for(int j=1; j<=stars; j++){
cout<<"*";
};
cout<<endl;
}
}
Interview problems
c++ code
void nStarTriangle(int n) {
// Write your code here.
for(int i=1;i<=(2*n-1);i++){
int stars=i;
if(i>n){
stars=(2*n-i);
}
for(int j=1;j<=stars;j++){
cout<<"*";
}
cout<<endl;
}
}Interview problems
solution
void nStarTriangle(int n) {
// Write your code here.
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
cout<<"*";
}
cout<<endl;
}
for(int i=0;i<n;i++){
for(int j=n;j>i;j--){
cout<<"*";
}
cout<<endl;
}
}
Interview problems
c++ code
void nStarTriangle(int n) {
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
for(int i=0;i<n;i++){
for(int j=n-1;j>i;j--){
cout<<"*";
}
cout<<endl;
}
}Interview problems
nstarTriangle
void nStarTriangle(int n) {
// Write your code here.
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
for(int i=1;i<=n;i++){
for(int j=1;j<n-i+1;j++){
cout<<"*";
}
cout<<endl;
}
}
Interview problems
Single for loop Python Solution
Single for loop (linear run)
def nStarTriangle(n: int) -> None:
# Write your code here.
j = 0
for i in range(1,2*n):
if i < n:
print('*'*i)
else:
print('*'*(n-j))
j += 1
Interview problems
Beginner Friendly Solution || C++
void nStarTriangle(int n) {
// Write your code here.
for(int i = 1;i<=n;i++){
for(int k = 0;k<i;k++){
cout<<"*";
}
cout<<endl;
}
for(int i = n-1;i>=1;i--){
for(int k = 0;k<i;k++){
cout<<"*";
}
cout<<endl;
}
}