Print a Reverse Left Half Pyramid Star Pattern

Solve this Problem
Easy10 min
Topics
Companies
Given an integer n, return the n rows of an upside-down right-aligned star pyramid — row i has (i − 1) leading spaces then (n − i + 1) stars. This is the Left Half Pyramid, flipped: the widest row (all n stars) comes first, narrowing by one star — and gaining one leading space — each row after. Both solutions carry over the Left Half Pyramid's technique directly, just with (i − 1) and (n − i + 1) swapped in for the space and star counts.

Test Case 1:

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

Test Case 2:

Input:n = 1
Output:["*"]
Explanation:A single row, no spaces needed.

Constraints

  • ◆1 ≤ n ≤ 20

Try the Dry Run

Approach & Solutions

Nested Loops — Spaces, Then StarsGood

For row i, first append (i − 1) spaces, then append (n − i + 1) stars. The upside-down version of the Left Half Pyramid: the widest row (all n stars, no spaces) comes first, shrinking by one star — and growing by one space — each row after.

TimeO(n²)
SpaceO(n²) for the output
1class Solution { 2 public String[] printReverseLeftHalfPyramid(int n) { 3 String[] result = new String[n]; 4 for (int i = 1; i <= n; i++) { 5 StringBuilder row = new StringBuilder(); 6 for (int s = 1; s <= i - 1; s++) { 7 row.append(' '); 8 } 9 for (int j = 1; j <= n - i + 1; j++) { 10 row.append('*'); 11 } 12 result[i - 1] = row.toString(); 13 } 14 return result; 15 } 16}
Built-in Repetition for Spaces and StarsOptimal

Build each half of the row with a built-in repeat: (i − 1) spaces concatenated with (n − i + 1) stars. C has no repeat built-in, so it keeps both loops.

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

Related Problems