AlgoViz

Pattern 2

Easy

Left-aligned right triangle

Problem

Print a left-aligned right triangle of stars for a given size n.

In simple words

Use one loop for the rows and another for what goes in each row to draw the shape.

The idea

Row i prints i stars, so the inner loop's bound depends on the outer counter rather than being fixed. That single change — inner bound tied to the row number — turns the square into a triangle.

The trick

  • Row i (1-based) has exactly i stars, so the inner loop runs `j <= i`.
  • Total stars printed is 1+2+…+n = n(n+1)/2, so the work is O(n²).
rows5

Step 1 of 7. Right triangle: row i prints i stars. rows 5.

1/7
Optimal
timeO(n^2)spaceO(1)
1// n = 5 produces:2// *3// * *4// * * *5// * * * *6// * * * * *7 8for i in 1..n:9  for j in 1..i: print('* ')10  newline

Input

grid
5 × 5

Memory

rows
5

Output

printed rows
rows
5

Check yourself

3 quick questions about this walkthrough. A wrong answer costs nothing.

Example

Input:
n = 5
Output:
★ / ★ ★ / ★ ★ ★ / ★ ★ ★ ★ / ★ ★ ★ ★ ★

Finished the walkthrough? Add it to your streak.