Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
What is Pattern shape programming?
4.2.
What is the structural pattern in Java?
4.3.
What are the different types of design patterns in Java?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Butterfly Shape Pattern

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
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 a butterfly as the problem's name.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Solution

We can use loops to solve the problem. We took two loops, one for the row and the next for the columns. We also took the control variable to control the design butterfly pattern. We also checked the printing condition. If an inner loop of the row is less than or equal to the control variable or the inner loop is more significant than the control difference, it will print the start. Otherwise, it will print a gap. It will also check for the condition if the outer loop is less than or equal to half of the row size. It will increment the control variable. Otherwise, it will decrease the count of the control variable.

Input Format

The first line of the input contains the row size of the Pattern, while the other variables controlled variable inner loop and outer loop counter variable.

Output Format

We are printing the pattern in the shape of a butterfly.

Example

import java.util.Scanner;
public class ButterflyStarPattern {


public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
        
       int out,in;
       int row_size=5;
       int print_control_x=1;
       for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in<=print_control_x || in>=row_size-print_control_x+1)
               {
                  System.out.printf("*");
               }
               else
               {
                System.out.printf(" ");    
               }
           }
           if(out<=row_size/2)
         print_control_x++;
           else
              print_control_x--;
           System.out.println(); 
       }
       cs.close();
}
}

Output

output
 

Here, we have printed the butterfly shape pattern.

Complexity Analysis

Time Complexity: O(N2)

Space Complexity: O(N)

Frequently Asked Questions

What is Pattern shape programming?

Shape Pattern is type programming where we draw the shapes which are repeated over and over again. This Pattern follows a particular order which makes it easier to follow.

What is the structural pattern in Java?

Structural design patterns simplify the design of large object structures by obtaining the relationship between them.

What are the different types of design patterns in Java?

These are creational, structural, and behavioral design patterns in Java.

Conclusion

In this article, we have learned to make the Butterfly shape pattern with the help of stars. We have briefly introduced the topic. We also have explained the problem statement and how we will approach the solution. We also demonstrated the working of loops step by step. We took an example regarding the problem of printing the shape. We were able to print the Pattern successfully. 

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.

Previous article
Cross Sign Pattern
Next article
Diamond Number Pattern
Live masterclass