The given pattern is a sort of hollow diamond, can be called a parallelogram.
In this problem, we encounter two types of spacing;
- Outer Spacing
- Inner Spacing
The outer spacing is responsible for the whitespaces outside the design. The inner spacing is responsible for the whitespaces inside the design pattern.
Analyzing a sample design for value of n =5:
Here, we can see the outer spacing decreases and then increases. Also outer spacing is initialized from a value half of n (n/2). The inner space on the first line doesn't exist, but then increases by an added factor of 2 in succeeding iterations and then decreases by the same factor as outer spacing increases.
We initialize inner space to be -1 because it is same thing as 0, meaning no space to be printed, but -1 here will serve a purpose as it will help us in increasing and decreasing inner spaces by 2.
Let us understand the spacing and star printing by writing a program; like we usually do, for printing the number of spaces and stars required on each line.
This program yields the following output:
This represents the number of outer spaces and inner spaces needed on each line respectively.