AlgoInsight

DSA problem

Butterfly Pattern

MediumPattern PrintingPattern: Composed LoopsbeginnerFree

Given an integer N, print a butterfly pattern of stars spanning 2 * N rows and 2 * N columns.

The butterfly is built from four triangles - two on the left and two on the right - with a gap that shrinks to zero at the middle and expands again. In the upper half (rows 1..N), row i has i stars, then 2 * (N - i) spaces, then i stars. The lower half mirrors the upper half by counting i back down from N to 1.

This tests your ability to manage several sections of output in a single row and to compose symmetric shapes.

Examples

Example 1
Input: N = 4
Output: * * ** ** *** *** ******** ******** *** *** ** ** * *
Explanation: Two blocks of i stars sit on each side of a shrinking gap of 2*(N - i) spaces; the lower half mirrors the upper.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

patternloops

Browse all DSA problems