Given a number n, print Butterfly pattern using '*' character. The pattern contains 2n - 1 rows, with the upper half expanding from 1 to n stars on each side and the lower half contracting from n - 1 to 1 star on each side, with spaces separating the two halves.
Examples:
Input: n = 4
Output:
* *
** **
*** ***
*******
*** ***
** **
* *
Explanation: The pattern contains 7 rows, expanding from 1 to 4 stars on each side and then contracting symmetrically.Input: n = 5
Output:
* *
** **
*** ***
**** ****
*********
**** ****
*** ***
** **
* *
Explanation: The pattern contains 9 rows, expanding from 1 to 5 stars on each side and then contracting symmetrically.
Nested Loops with Incremental and Decremental Stars - O(n ^ 2) Time and O(1) Space
The idea is to print the butterfly pattern using 2n - 1 rows.
- Maintain the number of stars on each side and the spaces between them.
- In the upper half, increase the stars by one and decrease the spaces by two in each row.
- In the lower half, decrease the stars by one and increase the spaces by two to create a symmetric butterfly pattern.
#include <bits/stdc++.h>
using namespace std;
void printButterflyPattern(int n)
{
int spaces = 2 * n - 1;
int stars = 0;
// Iterate through all 2*n - 1 rows.
for (int i = 1; i <= 2 * n - 1; i++)
{
// Update stars and spaces for the current row.
if (i <= n)
{
stars++;
spaces -= 2;
}
else
{
stars--;
spaces += 2;
}
// Print stars on the left side.
for (int j = 1; j <= stars; j++)
{
cout << "*";
}
// Print spaces between the two halves.
for (int j = 1; j <= spaces; j++)
{
cout << " ";
}
// Print stars on the right side.
for (int j = 1; j <= stars; j++)
{
if (j != n)
{
cout << "*";
}
}
// Move to the next line.
cout << endl;
}
}
int main()
{
int n = 4;
printButterflyPattern(n);
return 0;
}
class GFG {
static void printButterflyPattern(int n)
{
int spaces = 2 * n - 1;
int stars = 0;
// Iterate through all 2*n - 1 rows.
for (int i = 1; i <= 2 * n - 1; i++) {
// Update stars and spaces for the current row.
if (i <= n) {
stars++;
spaces -= 2;
}
else {
stars--;
spaces += 2;
}
// Print stars on the left side.
for (int j = 1; j <= stars; j++) {
System.out.print("*");
}
// Print spaces between the two halves.
for (int j = 1; j <= spaces; j++) {
System.out.print(" ");
}
// Print stars on the right side.
for (int j = 1; j <= stars; j++) {
if (j != n) {
System.out.print("*");
}
}
// Move to the next line.
System.out.println();
}
}
public static void main(String[] args)
{
int n = 4;
printButterflyPattern(n);
}
}
def printButterflyPattern(n):
spaces = 2 * n - 1
stars = 0
# Iterate through all 2*n - 1 rows.
for i in range(1, 2 * n):
# Update stars and spaces for the current row.
if i <= n:
stars += 1
spaces -= 2
else:
stars -= 1
spaces += 2
# Print stars on the left side.
for j in range(1, stars + 1):
print("*", end="")
# Print spaces between the two halves.
for j in range(1, spaces + 1):
print(" ", end="")
# Print stars on the right side.
for j in range(1, stars + 1):
if j != n:
print("*", end="")
# Move to the next line.
print()
# Driver Code
if __name__ == "__main__":
n = 4
printButterflyPattern(n)
using System;
class GFG {
static void printButterflyPattern(int n)
{
int spaces = 2 * n - 1;
int stars = 0;
// Iterate through all 2*n - 1 rows.
for (int i = 1; i <= 2 * n - 1; i++) {
// Update stars and spaces for the current row.
if (i <= n) {
stars++;
spaces -= 2;
}
else {
stars--;
spaces += 2;
}
// Print stars on the left side.
for (int j = 1; j <= stars; j++) {
Console.Write("*");
}
// Print spaces between the two halves.
for (int j = 1; j <= spaces; j++) {
Console.Write(" ");
}
// Print stars on the right side.
for (int j = 1; j <= stars; j++) {
if (j != n) {
Console.Write("*");
}
}
// Move to the next line.
Console.WriteLine();
}
}
static void Main()
{
int n = 4;
printButterflyPattern(n);
}
}
function printButterflyPattern(n)
{
let spaces = 2 * n - 1;
let stars = 0;
// Iterate through all 2*n - 1 rows.
for (let i = 1; i <= 2 * n - 1; i++) {
// Update stars and spaces for the current row.
if (i <= n) {
stars++;
spaces -= 2;
}
else {
stars--;
spaces += 2;
}
// Print stars on the left side.
for (let j = 1; j <= stars; j++) {
process.stdout.write("*");
}
// Print spaces between the two halves.
for (let j = 1; j <= spaces; j++) {
process.stdout.write(" ");
}
// Print stars on the right side.
for (let j = 1; j <= stars; j++) {
if (j !== n) {
process.stdout.write("*");
}
}
// Move to the next line.
console.log();
}
}
// Driver Code
const n = 4;
printButterflyPattern(n);
Output:
