The Butterfly Star Pattern (Combining Triangles)
Deconstruct the complex Butterfly pattern, mastering the alignment of left and right justified triangles within a composite grid.
The Ultimate Composite Test
The Butterfly pattern is often the final boss of university pattern exams. It looks like an intricate mirrored web of stars and spaces. If N = 3 (half height):
* *
** **
******
** **
* *
Deconstructing the Butterfly
Just like the Diamond, split the problem in half horizontally. The top half consists of 3 distinct sections per row:
- A left-aligned right triangle (Stars).
- A large block of hollow space (Spaces).
- A right-aligned right triangle (Stars).
The Math (Top Half)
For any row in the top half (where n = 3):
- Left Stars: Exactly equal to the
rownumber. - Spaces: The space in the middle is mathematically
2 * (N - row). - Right Stars: Exactly equal to the
rownumber.
The Implementation
int n = 3;
// Top Half
for (int row = 1; row <= n; row++) {
// 1. Left Stars
for (int i = 1; i <= row; i++) print("*");
// 2. Middle Spaces
for (int i = 1; i <= 2 * (n - row); i++) print(" ");
// 3. Right Stars
for (int i = 1; i <= row; i++) print("*");
println();
}
// Bottom Half (Exact same, just reverse the outer loop)
for (int row = n - 1; row >= 1; row--) {
for (int i = 1; i <= row; i++) print("*");
for (int i = 1; i <= 2 * (n - row); i++) print(" ");
for (int i = 1; i <= row; i++) print("*");
println();
}
The Takeaway
Never panic when presented with a complex visual problem. Break it into vertical chunks (left, middle, right) and horizontal chunks (top half, bottom half). Constructing the Butterfly pattern proves you can string together multiple mathematical boundaries flawlessly.
It requires coordinating three separate inner loops (left stars, middle spaces, right stars) per row, and mirroring the entire structure for the bottom half.
The total width is 2N. The stars take up 2 * row. Therefore, the formula for the remaining middle space is 2 * (N - row).
You copy the exact three inner loops from the top half, but place them inside an outer loop that iterates backwards from N-1 down to 1.
Yes, by generating a 2N x 2N grid and using if-else boundary checks on the diagonals, but the multi-loop segment approach is generally faster and easier to conceptualize.
To prevent the thickest middle row (where there are zero spaces) from being printed twice.
