AlgoInsight

DSA problem

Diamond Pattern of Stars

MediumPattern PrintingPattern: Composed LoopsbeginnerFree

Given an integer N, print a diamond of stars. The upper half is a pyramid of N rows and the lower half is an inverted pyramid of N - 1 rows.

A diamond is simply a pyramid stacked on top of an inverted pyramid. If you can build each half, you can build the diamond. The trick is the lower half must run from N - 1 down to 1 - not N - otherwise you duplicate the widest middle row.

This is your first taste of composing solutions: breaking a bigger shape into smaller, already-solved sub-shapes. That decomposition mindset is the foundation of divide-and-conquer and dynamic programming.

Examples

Example 1
Input: N = 4
Output: * *** ***** ******* ***** *** *
Explanation: Rows 1..4 form the upper pyramid; rows 3..1 form the lower inverted pyramid.

Constraints

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

patternloops

Browse all DSA problems