Table of contents
1.
Introduction
2.
Inverted Pant's Style Pattern
2.1.
Problem Statement
2.2.
Example
2.3.
Approach 
3.
Solution in C
4.
Solution in C++
5.
Solution in Java
6.
Solution in Python
7.
Complexity
7.1.
Time complexity
7.2.
Space Complexity
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Inverted Pant's Style Pattern

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

Introduction

Patterns are an excellent way to test the knowledge of loops in various programming languages. Pattern programs consist of integers, alphabets, or symbols in a specific order. The pattern questions are often asked questions in placements interviews.
 

Please check out our Top 150 Interview Puzzles if you are preparing for interviews.

 

We will now learn to solve the inverted pant's style pattern program and the solution in various languages like CC++Java, and Python.

Inverted Pant's Style Pattern

The inverted pant's style pattern program is asked in various interviews to test the candidate's programming skills. As the name suggests, the pattern is in the form of inverted pants. Let's discuss the problem statement and the solution in detail.

Problem Statement

Given a natural number n, print a pattern as an inverted pant with "n" rows.

Example

Let's look at some of the inputs and outputs to understand the pattern better.

 

Input:

n = 4

Output:

 

Input:

n = 5
Output:

Approach 

We will use the loops to solve the inverted pant's style pattern. The following steps have to be performed to obtain the pattern.

  • Take an outer for loop to print all the rows.
  • Take three inner for loop to print the first half of stars, spaces, and the second half of the stars.

Solution in C

Let's see the solution of the inverted pant's style pattern program in C.

 

Code:

// Include header file
#include <stdio.h>   

int main() {
    int n = 5;
    for (int row = n; row >= 0; row--) {
        // Prints first half of the stars
        for (int col = n; col > row; col--) {
            printf("* ");
        }
      
        // Prints space in between
        for (int col = 1; col <= 4 * row; col++) {
            printf(" ");
        }
      
        // Prints second half of the stars
        for (int col = row + 1; col <= n; col++) {
            printf("* ");
        }
      
        // Goes to a new line
        printf("\n");
    }
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Solution in C++

Let's see the solution of the inverted pant's style pattern program in C++.

 

Code:

// Include header file
#include <iostream>

int main() {
    int n = 5;
    for (int row = n; row >= 0; row--) {
        // Prints first half of the stars
        for (int col = n; col > row; col--) {
            std::cout << "* ";
        }
      
        // Prints space in between
        for (int col = 1; col <= 4 * row; col++) {
            std::cout << " ";
        }
      
        // Prints second half of the stars
        for (int col = row + 1; col <= n; col++) {
            std::cout << "* ";
        }
      
        // Goes to a new line
        std::cout << std::endl;
    }
    
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

 

To learn C++, check out our C++ guided path.

Solution in Java

Let's see the solution of the inverted pant's style pattern program in Java.

 

Code:

class Main  {  
    public static void main(String[] args)  {  
      int n = 5;


      for(int row = n ; row >= 0 ; row--) {
        // Prints first half of the stars
        for(int col = n ; col > row ; col--) {
          System.out.print("* ");
        }
        
        // Prints space in between
        for(int col = 1 ; col <= 4 * row ; col++) {
          System.out.print(" ");
        }


        // Prints second half of the stars
        for(int col = row + 1 ; col <= n ; col++) {
          System.out.print("* ");
        }


        // Goes to a new line
        System.out.println();
      }
    }  
}
You can also try this code with Online Java Compiler
Run Code

 

Output:


To learn Java, check out our Java guided path.

Solution in Python

Let's see the solution of the inverted pant's style pattern program in Python.


Code:

n = 5
row = n
while (row >= 0) :
    # Prints first half of the stars
    col = n
    while (col > row) :
        print("* ", end ="")
        col -= 1
        
    # Prints space in between
    col = 1
    while (col <= 4 * row) :
        print(" ", end ="")
        col += 1
        
    # Prints second half of the stars
    col = row + 1
    while (col <= n) :
        print("* ", end ="")
        col += 1
        
    # Goes to a new line
    print()
    row -= 1
You can also try this code with Online Python Compiler
Run Code

 

Output:


 

To learn Python, check out our Python guided path.

Complexity

Let's discuss the time complexity and space complexity of the above approach.

Time complexity

The time complexity is O(n2) as nested loops are used here. 

Space Complexity

The space complexity is O(1) as no extra space is used in the above program.
 

Now that you have learned how to solve the inverted pant's style pattern program check out the Pant's Style Pattern program.

FAQs

  1. What is a pattern program?
    Pattern programs consist of numbers, alphabets, or symbols in a particular form. The pattern programs can be solved easily using loops like the for loop, while loop, etc. 
     
  2. Why are pattern programs asked in interviews?
    Pattern programs are asked in interviews to judge the candidate's problem-solving ability and programming knowledge.
     
  3. Which are the commonly asked pattern-solving problems in interviews?
    In interviews, the commonly asked pattern-solving problems are Hollow Diamond Star Pattern, Inverted V-shape Number Pattern, Matrix in Reverse Spiral Form, etc.

Key Takeaways

In this article, we have extensively discussed the inverted pant's style pattern program and the solution in various languages like C, C++, Java, and Python.

 

We hope that this blog has helped you enhance your knowledge regarding the inverted pant's style pattern program, and if you would like to learn more, check out our Mock Test Series and Coding Interview Questions. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass