Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
Implementation
3.2.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
What is the use of a pattern program?
4.2.
What is the easiest way to solve a pattern problem?
4.3.
Why are Patterns essential to learn?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Pant's Style Pattern

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Problem Statement

In this pattern, you have to print using the stars(asterisk) in the shape of the pant as the name of the problem. 

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.

Live masterclass