Java ProgramsPatternsPrint Inverted Number Triangle

Print Inverted Number Triangle in Java

beginner·  Patterns  ·  Number Patterns

Problem

An inverted number triangle keeps the same row-relative counting as a plain number triangle, but starts each row at its widest and narrows it down one row at a time.

Given a number of rows, print a left-aligned triangle where the widest row comes first and each row still counts 1 up to its own width.

Input
n = 5
Output
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

Java Program

Java
public class InvertedNumberTriangle { public static void main(String[] args) { int n = 5; for (int i = n; i >= 1; i--) { // widest row first StringBuilder line = new StringBuilder(); for (int j = 1; j <= i; j++) { if (j > 1) line.append(" "); line.append(j); } System.out.println(line); } } }

Output

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

Core Logic

Counting the outer loop downward instead of upward keeps every row's own 1-to-width counting rule unchanged, while shrinking how wide each row gets.

How It Works
  1. 1The outer loop runs i from n down to 1, so the first row printed is the widest.
  2. 2The inner loop runs j from 1 to i, printing j at each step — the exact same row-content rule as the plain number triangle.
  3. 3Because i shrinks by one each time, every subsequent row has one fewer number than the last.
  4. 4The first row is 1 through n, and the final row is just 1.
For n = 5, the first row prints 1 2 3 4 5, and each row after that drops its final number, ending at a single 1.
💡

Key Point: Only the outer loop's direction changes here — the inner loop's own 1-to-i counting rule is identical to the plain number triangle, it just runs for fewer values as the rows go on.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The total count of numbers printed is still n+(n-1)+...+1, proportional to n², the same total work as the non-inverted version.

Key Concepts

nested for loopdecreasing row width

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class InvertedNumberTriangleStream { public static void main(String[] args) { int n = 5; IntStream.rangeClosed(1, n) .map(k -> n + 1 - k) // widest row first .mapToObj(i -> IntStream.rangeClosed(1, i) .mapToObj(String::valueOf) .collect(Collectors.joining(" "))) .forEach(System.out::println); } }

Output

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

Core Logic

Reusing the plain number triangle's exact per-row formula, but feeding it row indices in descending order instead of ascending, shrinks each row instead of growing it.

How It Works
  1. 1IntStream.rangeClosed(1, n).map(k -> n + 1 - k) produces the row indices n down to 1, the reverse of the plain triangle's ascending stream.
  2. 2For each row i, an inner IntStream.rangeClosed(1, i) still generates 1 through i — the identical row-content rule as the plain number triangle.
  3. 3.mapToObj(String::valueOf) converts each value to a String for joining.
  4. 4Collectors.joining(" ") joins that row's numbers before forEach prints it.
The first row index produced is 5, giving "1 2 3 4 5"; the last is 1, giving just "1".
💡

Key Point: Only the row-index stream's direction changes here — the exact same inner-stream mapping function from the plain triangle is reused unmodified, it just runs for fewer values as the outer stream progresses.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested streams still produce n+(n-1)+...+1 values total, proportional to n², regardless of visiting order.

Key Concepts

StreamIntStream.rangeClosed()Collectors.joining()

Related Programs