712033. Pattern 16

Easy

Pattern 16

Slug: triangle-pattern-1662285334 Difficulty: Easy Id: 712033

Summary

This problem is about generating a specific pattern involving triangles. The goal is to print a right-angled triangle with asterisks (*) in a specific format, where the number of rows and columns increases by one for each subsequent row. This requires a basic understanding of loops and conditional statements.

Detailed Explanation

To generate this pattern, we'll use nested loops to control the placement of asterisks. The outer loop will handle the rows, while the inner loop will handle the columns within each row.

Here's a step-by-step breakdown of the solution:

  1. Initialize an integer variable rows to 1 (the starting number of rows).
  2. Use a loop to iterate through each row:
    • For each row, use another loop to print spaces and asterisks.
      • Print the number of spaces equal to the current row number minus one (to create the right angle).
      • Then, print the number of asterisks equal to the current row number.
  3. Increment rows by 1 for each iteration.

The time complexity of this solution is O(n), where n is the number of rows in the triangle. The space complexity is O(1), as we only use a constant amount of space to store the variables.

Here's an ASCII art diagram of the pattern:

*
**
***
*****

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static void main(String[] args) {
        int rows = 1;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
            rows++;
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.