Last Updated : 05 Apr, 2023
In this article, we will learn about printing Left Triangle Star Pattern.
Examples:
Input : n = 5 Output: * * * * * * * * * * * * * * *
Left Triangle Star Pattern:
Java
import java.io.*;
// java program for left triangle
public class GFG {
// Function to demonstrate printing pattern
public static void StarleftTriangle(int k)
{
int a, b;
// 1st loop
for (a = 0; a < k; a++) {
// nested 2nd loop
for (b = 2 * (k - a); b >= 0; b--) {
// printing spaces
System.out.print(" ");
}
// nested 3rd loop
for (b = 0; b <= a; b++) {
// printing stars
System.out.print("* ");
}
// end-line
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int k = 5;
StarleftTriangle(k);
}
}
* * * * * * * * * * * * * * *
Time Complexity: O(k2), where k represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
import java.io.*;
class GFG
{
// similar to inner for loop
public static void printRow(int n, int totalRows)
{
if(totalRows == 0)
{
return;
}
if(totalRows <= n) // condition when to print star
{
System.out.print(" *");
}
else // condition when to print space
{
System.out.print(" ");
}
printRow(n, totalRows - 1); // moving to next column recursively
}
// similar to outer for loop
public static void newRow(int n, int totalRows)
{
if(n == 0)
{
return;
}
newRow(n - 1, totalRows);
printRow(n, totalRows); // prints stars and space in a single row
System.out.print("\n"); // for new Row.....new line
}
public static void main (String[] args)
{
GFG.newRow(5, 5); // newRow is static method so no need to create an object of GFG
}
}
* * * * * * * * * * * * * * *
Time Complexity: O(n)
Auxiliary Space: O(n), Due to recursion stack in memory.
The idea behind this code is to print a left triangle pattern of stars with a given number of rows.
Step-by-step approach to implement this idea:
The code above follows this approach by using two nested for loops. The outer loop is responsible for iterating through each row, while the inner loop is responsible for printing the stars in each row. The number of stars printed in each row is determined by the value of the outer loop variable i.
Java
public class LeftTrianglePattern {
public static void main(String[] args) {
int numRows = 5;
for (int i = 1; i <= numRows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
* * * * * * * * * * * * * * *
Time Complexity:
The outer loop runs numRows times, while the inner loop runs i times in each iteration of the outer loop. Therefore, the total number of iterations is 1 + 2 + 3 + ... + numRows, which is equal to numRows(numRows+1)/2. Therefore, the time complexity of the code is O(numRows^2).
Auxiliary Space:
The code uses a constant amount of auxiliary space to store the values of numRows, i and j. Therefore, the auxiliary space complexity of the code is O(1).
Approach Name: Using nested for loopsSteps:
public class LeftTriangleStarPattern {
public static void printLeftTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
int n = 5;
printLeftTriangle(n);
}
}
* * * * * * * * * * * * * * *
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Approach: Java 8 Stream-based Left Triangle Star Pattern Printer
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class LeftTriangleStarPattern {
public static void main(String[] args)
{
int n = 5;
IntStream.range(1, n + 1).forEach(row -> {
String stars
= IntStream.range(1, row + 1)
.mapToObj(i -> "* ")
.collect(Collectors.joining());
System.out.printf("%" + n * 2 + "s%n", stars);
});
}
}
* * * * * * * * * * * * * * *
Time complexity: O(n2)
Auxiliary space: O(n)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4