711658. Pattern 6
Here is the Markdown blog post for the GeeksforGeeks problem "Pattern 6":
711658: Pattern 6
Summary
This problem involves generating a triangular pattern based on a given input. The key concept involved is array manipulation, specifically building and printing triangles.
Detailed Explanation
The problem requires us to generate a triangular pattern of asterisks (*) where the number of rows (n) is provided as input. Each row should have one more asterisk than the previous row. For example, if n = 5, the output would be:
*
**
***
****
*****
To solve this problem, we can use a simple loop that iterates over each row of the triangle and prints the corresponding number of asterisks.
Here's the step-by-step breakdown:
- Initialize an integer variable
ito keep track of the current row. - Start a loop that runs from 1 to n (inclusive).
- Inside the loop, print
iasterisks followed by a newline character (\n) to start a new line. - Increment
ifor each iteration of the loop.
Time complexity: O(n), where n is the input number of rows. Space complexity: O(1), as we only use a constant amount of space to store the current row index and other variables.
Optimized Solutions
Java
public class Pattern6 {
public static void main(String[] args) {
int n = 5; // input number of rows
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}