Last Updated: June 7, 2026
A hollow triangle keeps only the outline of a left-aligned right triangle. Each row starts at the left margin and grows one character wider than the row above it. Row 1 has one character, row 2 has two, and row n has n.
Three parts keep their stars: the left vertical edge, which is the first column of every row, the slanted right edge, which is the last position of each row, and the bottom edge, which is the entire final row. Anything off these three edges becomes a space.
For n = 5, look at row 3. It has three positions. The first column is an edge, the third position is the slanted edge for that row, and the middle is interior. So row 3 is * *, a star, a space, then a star. The final row, row 5, is the base, so all five positions are stars and it reads *****.
1 <= n <= 100 → n is always at least 1, so there is always a first row. The sizes are small, so building rows one character at a time is fast enough.i has exactly i characters, and the output is not padded to a common width.* and row 2 is **, with no interior to hollow out.The shape is a rule about each position, so the most direct method is to visit every position and apply it. Walk the rows from 1 to n, and within each row walk the columns from 0 to i - 1.
A position is on an edge when j == 0, j == i - 1, or i == n. If any of those holds, write a *. Otherwise the position is interior, so write a space.
i from 1 to n, where i is both the row number and its length.j from 0 to i - 1.j == 0, j == i - 1, or i == n, append a *, otherwise append a space.Input:
With n = 4, the loop builds four rows. Row 1 has one position at j = 0, which satisfies j == 0, so it is *. Row 2 has positions j = 0 and j = 1. Both are edges since one is the first column and the other is the last, so the row is **. Row 3 has positions 0, 1, 2. Here j = 0 is the first column and j = 2 is the last, but j = 1 is interior and i is not yet the bottom row, so the row is * *. Row 4 is the bottom row, so i == n holds for every position and the row is the solid base ****. Together they form the hollow triangle:
i does i units of work, and the rows sum to 1 + 2 + ... + n, which grows with the square of n.1 + 2 + ... + n, so the total characters stored scale with the square of n.Testing every position works, but the stars sit in a small number of predictable places. Can we place those stars directly instead of checking each position?
Instead of deciding every position, place the stars directly. Any row that is not the bottom row has only two filled positions, the first column and the last, so it is a star, a run of spaces, then a star. The bottom row is the solid base, every position a star.
To build a row above the base, start with i spaces, then set the first and last characters to stars. Rows of length 1 and 2 have no gap between their two ends, so they fill up on their own. The bottom row skips the gap and is just i stars.
i from 1 to n.i == n, this is the base, so add a row of i stars and continue.i spaces.0) to a star.i - 1) to a star. For i == 1 this is the same position, which stays a single star.Input:
With n = 4, rows 1 through 3 are above the base. Row 1 starts as one space, then index 0 and the last index both point to position 0, leaving a single *. Row 2 starts as two spaces, and setting index 0 and index 1 to stars fills it to **. Row 3 starts as three spaces, and setting index 0 and index 2 to stars gives * *, with the middle space untouched. Row 4 is the base, so it is added as four stars, ****. The assembled rows match the hollow triangle:
i is created by allocating and filling i characters, and the lengths sum to 1 + 2 + ... + n, which grows with the square of n.1 + 2 + ... + n, so the stored characters scale with the square of n.