AlgoMaster Logo

Floyd's Triangle

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

Floyd's Triangle writes the counting numbers in a triangular shape. You start at 1 in the top-left corner and keep counting upward, while the rows grow as you go down: the first row holds one number, the second holds two, the third holds three, for as many rows as you ask for.

The numbers never restart. Once you write 1 in the first row, the next row picks up at 2 and 3, the row after that runs 4, 5, 6, and so on. So the value in any cell depends only on how many numbers came before it, not on which row it sits in.

For n = 3 the triangle is [[1], [2, 3], [4, 5, 6]], and for n = 1 it is [[1]]. The task is to return these rows as a list, where each inner list is one row.

Key Constraints:

  • 1 <= n <= 100n is always at least 1, so the triangle has at least one row and is never empty. The number of rows is exactly n.
  • Row i contains i numbers → Row 1 has one value, row 2 has two, and the last row has n values. The total count of numbers is 1 + 2 + ... + n, which equals n × (n + 1) / 2.
  • The values fit in a 32-bit int → At n = 100 the largest number is 5050, far below the integer limit, so there is no overflow concern.

Approach 1: Running Counter

Intuition

Since the numbers flow continuously from one row into the next, carry a single counter across all the rows. Start it at 1, and each time you place a value into a cell, write the counter and then increment it.

The rows only change how many cells you fill before moving on. Row i needs i numbers, so you fill i cells from the shared counter and then start a new row. Because the counter is never reset, row 2 begins where row 1 left off.

Algorithm

  1. Create an empty list triangle to hold the rows.
  2. Start a counter num at 1.
  3. For each row index i from 1 to n:
    • Create an empty row.
    • Repeat i times: add num to the row, then increase num by one.
    • Add the completed row to triangle.
  4. Return triangle.

Example Walkthrough

Input:

3
n

The counter num starts at 1. Row 1 needs one number, so it takes 1 and the counter moves to 2. Row 2 needs two numbers, so it takes 2 and 3, leaving the counter at 4. Row 3 needs three numbers, so it takes 4, 5, and 6. After the third row the loop ends, and the rows collected so far form the triangle:

0
[1]
1
[2,3]
2
[4,5,6]
result

Code

The running counter ties the rows together, but it forces you to fill the rows in order so the counter stays correct. What if you wanted to compute any row on its own, without filling the rows above it first?

Approach 2: Row-Start Formula

Intuition

Each row starts right after the previous rows end, so the first number in a row depends only on how many numbers came before it. Rows 1 through i - 1 together hold 1 + 2 + ... + (i - 1) numbers, which is the triangular number i × (i - 1) / 2. The first value of row i is one more than that count, so it equals i × (i - 1) / 2 + 1.

Once you know where a row begins, filling it is easy. The numbers in a row are consecutive, so row i runs from its starting value through the next i - 1 values. This lets you build each row directly from its index without carrying any state from earlier rows.

Algorithm

  1. Create an empty list triangle to hold the rows.
  2. For each row index i from 1 to n:
    • Compute the starting value start as i × (i - 1) / 2 + 1.
    • Build a row of i consecutive numbers beginning at start.
    • Add the row to triangle.
  3. Return triangle.

Example Walkthrough

Input:

3
n

For row 1, the start is 1 × 0 / 2 + 1 = 1, so the row is [1]. For row 2, the start is 2 × 1 / 2 + 1 = 2, and the two consecutive numbers are 2 and 3. For row 3, the start is 3 × 2 / 2 + 1 = 4, and the three consecutive numbers are 4, 5, and 6. Each row was computed from its index alone, and together they form the triangle:

0
[1]
1
[2,3]
2
[4,5,6]
result

Code