AlgoMaster Logo

Inverted Right-Angled Triangle

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

This is the upright right-angled triangle turned upside down. The widest row sits at the top, and the point of the triangle ends at the bottom.

The first row has n stars, and each row below has one fewer than the row above. The last row, row n, has a single star. For n = 4, the rows hold 4, 3, 2, and 1 stars, which reads as ****, ***, **, *. Each row is its own string in the returned array.

Row i has n - i + 1 stars. When i is 1, that gives n stars for the top row, and when i is n, it gives 1 star for the bottom row. Getting that formula right is the heart of the problem.

Key Constraints:

  • 1 <= n <= 100n is always at least 1, so the result has at least one row and is never empty. The widest row has at most 100 stars, which stays small.
  • Each row is left-aligned → No leading or trailing spaces appear on any row. Row i is simply n - i + 1 stars joined together.

Approach 1: Nested Loops (Counting Down)

Intuition

An outer loop walks the row numbers from 1 to n. For each row i, the star count is n - i + 1, so an inner loop runs that many times and appends a star on each pass. At the top, i is small, so the count is close to n. As i climbs toward n, the count drops toward 1, producing the shrinking shape.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop i from 1 to n, where i is the current row number.
  3. Compute the star count for this row as n - i + 1.
  4. Build a string of that many stars with an inner loop and add it to the list.
  5. After the outer loop ends, return the list.

Example Walkthrough

Input:

4
n

At i = 1, the count is 4 - 1 + 1 = 4, so the inner loop builds "****" for the first row. At i = 2, the count is 3, giving "***". At i = 3 the count is 2, giving "**", and at i = 4 the count is 1, giving "*". The four rows collected in order form the inverted triangle:

0
****
1
***
2
**
3
*
result

Code

The counting-down formula works, but there is a second way to see this shape. An inverted triangle is just an upright triangle with its rows flipped top to bottom. Can we build the upright version and flip it?

Approach 2: Build and Reverse

Intuition

The inverted triangle holds the same rows as the upright triangle, just read backward. The upright version lists them shortest to longest as *, **, ***, ****, while the inverted version lists them longest to shortest as ****, ***, **, *.

So build the upright triangle first, where row i has i stars, then reverse the order of the rows. The reversal turns the ascending sequence of lengths into a descending one, which is the inverted shape. This keeps the row-building logic plain and leaves the flipping to a single reverse step.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop i from 1 to n and add a string of i stars to the list, building the upright triangle.
  3. Reverse the list so the longest row comes first and the shortest comes last.
  4. Return the reversed list.

Example Walkthrough

Input:

4
n

The first loop builds the upright triangle row by row, producing ["*", "**", "***", "****"]. The rows climb from one star to four. Reversing this list swaps the first and last rows, then the second and third, leaving the longest row on top and the single star at the bottom:

0
****
1
***
2
**
3
*
result

Code