Given an integer N, create an inverted left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and each following row has one star less than the previous row, until the last row contains only 1 star. All stars are right-aligned.

Examples:
Input: N = 3
Output:
***
**
*Input: N = 5
Output:
*****
****
***
**
*
Try It Yourself
Using Nested Loops – O(N²) Time and O(1) Space
The inverted left half pyramid pattern can be printed using two nested loops inside an outer loop. The outer loop controls the rows, the first inner loop prints i - 1 spaces, and the second inner loop prints N - i + 1 stars in the i-th row.
Step-by-Step Approach
- Run the outer loop from 1 to N to represent the rows.
- For each row i, run the first inner loop from 1 to i - 1 and print a space (' ') in each iteration.
- Run the second inner loop from 1 to N - i + 1 and print a star ('*') in each iteration.
- After printing spaces and stars for the current row, print a newline to move to the next row.
- Repeat this process until all N rows are printed.
using namespace std;
int main()
{
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++) {
// Inner loop prints 'i - 1' spaces
for (int j = 1; j <= i - 1; j++) {
cout << " ";
}
// Inner loop prints 'N - i + 1' stars
for (int j = 1; j <= N - i + 1; j++) {
cout << "*";
}
cout << "\n";
}
return 0;
}
public class GFG {
public static void main(String[] args) {
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++) {
// Inner loop prints 'i - 1' spaces
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
// Inner loop prints 'N - i + 1' stars
for (int j = 1; j <= N - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
def main():
N = 5
# Outer loop runs N times, once for each row
for i in range(1, N + 1):
# Inner loop prints 'i - 1' spaces
for _ in range(i - 1):
print(" ", end="")
# Inner loop prints 'N - i + 1' stars
for _ in range(N - i + 1):
print("*", end="")
print()
if __name__ == "__main__":
main()
using System;
class GFG
{
static void Main()
{
int N = 5;
// Outer loop runs N times, once for each row
for (int i = 1; i <= N; i++)
{
// Inner loop prints 'i - 1' spaces
for (int j = 1; j <= i - 1; j++)
{
Console.Write(" ");
}
// Inner loop prints 'N - i + 1' stars
for (int j = 1; j <= N - i + 1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
const N = 5;
// Outer loop runs N times, once for each row
for (let i = 1; i <= N; i++) {
// Inner loop prints 'i - 1' spaces
for (let j = 1; j <= i - 1; j++) {
process.stdout.write(" ");
}
// Inner loop prints 'N - i + 1' stars
for (let j = 1; j <= N - i + 1; j++) {
process.stdout.write("*");
}
console.log();
}
Output
*****
****
***
**
*