Print a Reverse Right Half Pyramid Star Pattern

Solve this Problem
Easy5–10 min
Topics
Companies
Given an integer n, return the n rows of an upside-down left-aligned star pyramid — row i has (n − i + 1) stars, shrinking one star per row. This is the Right Half Pyramid, flipped: instead of growing from 1 star to n, it shrinks from n stars down to 1. Both solutions carry the exact same technique over — just with (n − i + 1) in place of i wherever the row length is needed.

Test Case 1:

Input:n = 4
Output:["****", "***", "**", "*"]
Explanation:Row i has (n − i + 1) stars, left-aligned — shrinking one star per row.

Test Case 2:

Input:n = 1
Output:["*"]
Explanation:A single row, a single star.

Constraints

  • ◆1 ≤ n ≤ 20

Try the Dry Run

Approach & Solutions

Nested Loops — Print Each CharacterGood

For row i (from 1 to n), run an inner loop that appends (n − i + 1) stars — the upside-down version of the Right Half Pyramid: the widest row comes first, shrinking by one star each time.

TimeO(n²)
SpaceO(n²) for the output
1class Solution { 2 public String[] printReverseRightHalfPyramid(int n) { 3 String[] result = new String[n]; 4 for (int i = 1; i <= n; i++) { 5 StringBuilder row = new StringBuilder(); 6 for (int j = 1; j <= n - i + 1; j++) { 7 row.append('*'); 8 } 9 result[i - 1] = row.toString(); 10 } 11 return result; 12 } 13}
String Repetition per RowOptimal

Row i is just (n − i + 1) stars — build it directly with a built-in repeat instead of an inner loop. C has no repeat built-in, so it keeps both loops.

TimeO(n²)
SpaceO(n²)
1class Solution { 2 public String[] printReverseRightHalfPyramid(int n) { 3 String[] result = new String[n]; 4 for (int i = 1; i <= n; i++) { 5 result[i - 1] = "*".repeat(n - i + 1); 6 } 7 return result; 8 } 9}

Related Problems