Hollow Diamond Pattern

Last Updated : 11 Mar, 2026

Given an integer N, print the pattern shown below.

Examples:

Input: N=5

Output:

star-pattern

Input: N=3

Output:

Screenshot-2026-03-10-181656
Try it on GfG Practice
redirect icon

Using Nested Loops - O(n^2) Time and O(1) Space

  • Iterate through 2 × n − 1 rows to form the upper and lower parts of the pattern, and compute a variable (comp) to control the spacing for each row.
  • Print leading spaces using an inner loop so that the stars shift toward the center and form the desired shape.
  • Use another inner loop to print stars at the first and last positions of the row and spaces in between, creating a hollow pattern.
C++
#include <iostream>
using namespace std;

int main() {
    int n = 3;

    // Outer loop for rows
    for (int i = 0; i < 2 * n - 1; i++) {

        int comp;
        if (i < n)
            comp = 2 * (n - i) - 1;
        else
            comp = 2 * (i - n + 1) + 1;

        // Print leading spaces
        for (int j = 0; j < comp; j++)
            cout << " ";

        // Print stars and inner spaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1)
                cout << "* ";
            else
                cout << "  ";
        }

        cout << endl;
    }

    return 0;
}
Java
public class Gfg{
    public static void main(String[] args) {
        int n = 3;

        // Outer loop for rows
        for (int i = 0; i < 2 * n - 1; i++) {

            int comp;
            if (i < n)
                comp = 2 * (n - i) - 1;
            else
                comp = 2 * (i - n + 1) + 1;

            // Print leading spaces
            for (int j = 0; j < comp; j++)
                System.out.print(" ");

            // Print stars and inner spaces
            for (int k = 0; k < 2 * n - comp; k++) {
                if (k == 0 || k == 2 * n - comp - 1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }

            System.out.println();
        }
    }
}
Python
def main():
    n = 3

    # Outer loop for rows
    for i in range(2 * n - 1):

        comp = 2 * (n - i) - 1 if i < n else 2 * (i - n + 1) + 1

        # Print leading spaces
        for j in range(comp):
            print(' ', end='')

        # Print stars and inner spaces
        for k in range(2 * n - comp):
            if k == 0 or k == 2 * n - comp - 1:
                print('* ', end='')
            else:
                print('  ', end='')

        print()


if __name__ == "__main__":
    main()
C#
using System;

public class GfG
{
    public static void Main()
    {
        int n = 3;

        // Outer loop for rows
        for (int i = 0; i < 2 * n - 1; i++)
        {
            int comp = i < n ? 2 * (n - i) - 1 : 2 * (i - n + 1) + 1;

            // Print leading spaces
            for (int j = 0; j < comp; j++)
                Console.Write(" ");

            // Print stars and inner spaces
            for (int k = 0; k < 2 * n - comp; k++)
            {
                if (k == 0 || k == 2 * n - comp - 1)
                    Console.Write("* ");
                else
                    Console.Write("  ");
            }

            Console.WriteLine();
        }
    }
}
JavaScript
function printPattern(n) {

    // Outer loop for rows
    for (let i = 0; i < 2 * n - 1; i++) {

        let comp = i < n ? 2 * (n - i) - 1 : 2 * (i - n + 1) + 1;

        // Print leading spaces
        let spaces = ' '.repeat(comp);

        let stars;
        if (2 * n - comp === 1) {
            stars = '*';  // single star case
        } else {
            stars = '*' + ' '.repeat((2 * n - comp - 2) * 2) + '*';
        }

        console.log(spaces + stars);
    }
}

// Function call
printPattern(3);

Output
     * 
   *   * 
 *       * 
   *   * 
     * 
Comment