AlgoViz

Pattern 6

Easy

Inverted number triangle

Problem

Print an inverted number triangle 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

The inverted triangle shape with the column counter printed instead of a star. Shape and content stay independent: pick the bound for the shape, pick the printed value for the content.

The trick

  • Row i prints 1..(n-i+1).
rows5

Step 1 of 7. Inverted number triangle: row i prints 1..(n-i+1). rows 5.

1/7
Optimal
timeO(n^2)spaceO(1)
1// n = 5 produces:2// 1 2 3 4 53// 1 2 3 44// 1 2 35// 1 26// 17 8for i in 1..n:9  for j in 1..(n-i+1): print(j)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:
1 2 3 4 5 / 1 2 3 4 / 1 2 3 / 1 2 / 1

Finished the walkthrough? Add it to your streak.