Solution
We can use loops to solve this problem. We will be taking two loops for each of the iterations. The outer loop will cover the rows, and the inner loop will traverse through columns.
The outer loop will iterate to the size of the pant, while the inner one will move twice the size of the pant. Then we have set two control to print the stars, one in the vertical direction while the other in the horizontal direction. If both controls are in the same number, it will print a gap. If the gap is not the same, it will print the star.
Input Format
The first line contains the row size of the pattern, while the other variables were for the internal and external of the pattern.
Output Format
We are printing the pattern in the form of a pant.
Implementation
import java.util.Scanner;
public class Pant_Style_Pattern {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int row_size = 5;
int out,in;
int print_control_x=row_size;
int print_control_y=row_size;
for(out=1;out<=row_size;out++)
{
for(in=1;in<row_size*2;in++)
{
if(in>print_control_x && in<print_control_y)
{
System.out.printf(" ");
}
else
{
System.out.printf("*");
}
}
print_control_x--;
print_control_y++;
System.out.println();
}
cs.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
*********
**** ****
*** ***
** **
* *
In this example, the output will be a pattern in the form of a pant.
Complexity Analysis
Time Complexity: O(N^2)
Because we iterate over nested for loops with each of them iterating from 0 to N where N is the length of the pant
Space Complexity: O(1)
Because no extra storage space is needed.
Frequently Asked Questions
What is the use of a pattern program?
Pattern programs are the best way to study the loop structure in general programming. It enables us to visualize how the iteration works in a loop.
What is the easiest way to solve a pattern problem?
First, we need to make a table to look for patterns, find the relationship between the patterns, make a prediction and then check the answer.
Why are Patterns essential to learn?
Patterns help us make predictions of what will happen. Next, It helps us develop logical connections and improve reasoning skills.
Conclusion
In this article, we have learned the Pant's Style Pattern using the stars. We have briefly introduced the topic. We also have given a brief idea of our problem statement of how we will approach this problem. We also explained it is working and how it will go through step by step. We also took the example and the output of the pattern.
Recommended Readings:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.