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

Rotated Triangle

Easy
0/40
profile
Contributed by
100 upvotes

Problem statement

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: 

*
**
***
**
*
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 printing every cell using ‘*’ or spaces.

The steps are as follows :

Function void nStarTriangle(int ‘N’)

 

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

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 ). 

Space Complexity

O(1)

 

Since we are using constant extra space, the space complexity is of the order O(1).

Code Solution
(100% EXP penalty)
Rotated Triangle
All tags
Sort by
Search icon

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();

}

2 views
0 replies
0 upvotes

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;

        }

    }

}

17 views
0 replies
0 upvotes

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()
    pass

beginners

python

16 views
0 replies
0 upvotes

Interview 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;

    }

       

}

25 views
0 replies
0 upvotes

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;
    }
}
19 views
0 replies
0 upvotes

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;

    }

 

}

11 views
0 replies
1 upvote

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;
    }
}
8 views
0 replies
0 upvotes

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;

    }

 

}

11 views
0 replies
0 upvotes

Interview problems

Single for loop Python Solution

Time Complexity: O(n)

 

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

python

18 views
0 replies
0 upvotes

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;

    }

 

}

34 views
0 replies
1 upvote
Full screen
Console