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

Reverse Star Triangle

Easy
0/40
profile
Contributed by
106 upvotes

Problem statement

Ninja was very fond of patterns. For a given integer ‘N’, he wants to make the Reverse N-Star 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 output as specified.
Constraints :
1  <= N <= 20
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
*****
 ***
  *
Explanation Of Sample Input 1 :
The first row contains five stars.
The second row contains one space, followed by three stars.
The third row contains two spaces, followed by a star.
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 nStar(int ‘N’)

 

  1. Int ‘gap’ = 0, ‘stars’ = 2*’N’-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
Time Complexity

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

Two nested loops are running 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)
Reverse Star Triangle
All tags
Sort by
Search icon

Interview problems

cpp solution

void nStarTriangle(int n) {
    // Write your code here.
    for (int i=0;i<n;i++)
    {
      for (int j = 0; j < i; j++) {
        cout << " ";
      }

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

          for (int j=0;j<i;j++)
        {
            cout<<" ";
        }
        cout<<endl;
    }
}
48 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<<" ";

        }

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

            cout<<"*";

 

        }

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

            cout << " ";

        }

        cout<<endl;

    }

}

19 views
0 replies
2 upvotes

Interview problems

c++

void nStarTriangle(int n) {
    for(int i=0;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;
    }
}
20 views
0 replies
0 upvotes

Interview problems

Pattern

void nStarTriangle(int n) {

    // Write your code here.

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

        //space

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

            cout<<" ";

        }

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

            cout<<"*";

        }

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

            cout<<" ";

        }

        cout<<endl;

    }

}

20 views
0 replies
0 upvotes

Interview problems

solved in cpp

void nStarTriangle(int n) {

    // Write your code here.

    

     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;

 

}

   

}

7 views
0 replies
0 upvotes

Interview problems

Reverse N-Star Triangle

void nStarTriangle(int n) {

    // Write your code here.

    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;

    }

}

19 views
0 replies
0 upvotes

Interview problems

Beginner Friendly Solution || C++

void nStarTriangle(int n) {

    // Write your code here.

 

    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;

    }

}

17 views
0 replies
0 upvotes

Interview problems

Reverse Star Triangle Solution in Java

public class Solution {
    public static void nStarTriangle(int n) {
        // Write your code here
        for(int i=n; i>=1; i--){
            // for spaces
            for(int j=(n-i); j>0; j--){
                System.out.print(" ");
            }
            // for stars
            for(int k=1; k<=(2*i-1); k++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

java

beginners

programming

Coding Ninjas

+1 more
41 views
0 replies
0 upvotes

Interview problems

Python code with only one loop.

def nStarTriangle(n: int) -> None:
    i = n
    spaces = 0
    while i >= 0:
        print(spaces * ' ' + (2*i - 1) * '*' + spaces * ' ', end='')
        print()
        spaces += 1
        i -= 1

This is just the opposite of the last problem, where you need to print the opposite pattern.

27 views
0 replies
0 upvotes

Interview problems

Java code with 3 loops

public class Solution {

    public static void nStarTriangle(int n) {

        // Write your code here

 

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

 

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

                 System.out.print(" ");

             }

 

         

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

                 System.out.print("*");

 

             }

             

             System.out.println();

         }

 

         

    }

}

15 views
0 replies
0 upvotes
Full screen
Console