Void of Diamond

Easy
0/40
5 upvotes

Problem statement

You are given an integer β€˜N’, β€˜N’ will always be an odd integer. Your task is to print a pattern with the following description:

1. The pattern will consist of β€˜N’ lines.
2. The pattern will consist of β€˜ β€˜ (space) and β€˜*’ characters only.
3. The pattern will be a β€œVoid of Diamond” pattern.
4. A β€œVoid of Diamond” pattern is a pattern β€˜N’ * β€˜N’ cells and β€˜ β€˜ characters make a diamond shape and β€˜*’ fill all other points.
5. For a better understanding of the β€œVoid of Diamond” pattern refer to example and sample input-output.

For example:

If β€˜N’ is 5 then the pattern will be-

*****
** **
*   *
** **
*****
Detailed explanation ( Input/output format, Notes, Images )

Input Format:

The first line of input contains an integer 'T' representing the number of test cases.

Then each test case contains a single integer β€˜N’ denoting the size of the pattern.

Output Format:

For each test case, print 'N' strings denoting the pattern.

The output of each test case will be printed in a separate line.

Constraints:

1 <= T <= 5
3 <= N <= 500

Where β€˜T’ is the number of test cases, β€˜N’  is the size of the pattern. β€˜N’ will be odd for all test cases.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Sample Input 1:

2
5
3

Sample Output 1:

*****
** **
*   *
** **
*****
***
* *
***

Explanation of Sample Input 1:

Test Case 1:

Given β€˜N’ = 5
We will print the pattern as the description of the β€œVoid of Diamond” pattern.

Test Case 2:

Given β€˜N’ = 3
There will be only 1 β€˜ β€˜ space in the diamond pattern.

Sample Input 2:

2
7
9

Sample Output 2:

*******
*** ***
**   **
*     *
**   **
*** ***
*******
*********
**** ****
***   ***
**     **
*       *
**     **
***   ***
**** ****
*********

Explanation of Sample Input 2:

Test Case 1:

Given β€˜N' = 7
The pattern is printed such that β€˜ β€˜ making a diamond and β€˜*’ filling other points.

Test Case 2:

Given β€˜N’ = 9
Created a 9*9 grid and all space cells make a diamond pattern.
Hint

We can see the β€˜N’ * β€˜N’ grid as an x-y plane, and can easily divide it into 4 symmetric parts and is there any relation of β€˜ β€˜ characters with coordinates?

Approaches (1)
Pattern printing

The idea is here to visualize the problem in a different way, here β€˜N’ will be odd so along the middle vertical line and middle horizontal this β€˜N’ * β€˜N’ grid can be divided into 4 symmetric parts and we can see it as an x-y plane. So we can see this β€˜N’ * β€˜N’ grid as a square on coordinate axis with the bottom left corner at { -((β€˜N’ - 1) / 2),  -((β€˜N’ - 1) / 2)} and upper right corner at { (β€˜N’ - 1) / 2 ,  (β€˜N’ - 1) / 2}.

Now if we assign coordinates to all other points as {x, y} we will find that in a quadrant all β€˜ β€˜ cells will form a triangular region and if we combined them all we will get a mathematical equation as |x| + |y| < (β€˜N’ - 1) / 2.

So now we run 2 loops for all coordinates and make a character β€˜ β€˜ if the sum of the absolute value of coordinates is less than ( β€˜N’ - 1) / 2 otherwise it will be a β€˜*’ character and here we can say in this case we will generate the pattern in a way such that first, we will generate the bottom line then will move till the top line but actually it won’t affect our pattern because it is symmetric with the horizontal bisector.

 

Algorithm:

 

  • Create an array of strings for storing the pattern say β€˜answer’.
  • Run a loop from β€˜i’ = -(β€˜N’ - 1)/2  to β€˜i’ = (β€˜N’ -1)/2
  • Create a string β€˜str’ for storing the current row of pattern.Run a loop from β€˜j’ = -(β€˜N’ - 1) / 2  to β€˜j’ = (β€˜N’ - 1) / 2.
  • If absolute(i) + absolute(j) is less than (β€˜N’ - 1) / 2 then append a β€˜ β€˜ character to the β€˜str’, else append β€˜*’ to the β€˜str’.
  • Append β€˜str’ to β€˜answer’.

 

  • Return the β€˜answer’ array that contains the pattern.
Time Complexity

O(N ^ 2), where N is the size of the pattern.

 

We are traversing each point of the β€˜N’ * β€˜N’ grid so we are doing β€˜N’ ^ 2 iterations here so the overall time complexity is O(N ^ 2).

Space Complexity

O(N ^ 2), where N is the size of the pattern.

 

As we will be storing the result in an array of strings that contains β€˜N’  strings of size β€˜N’ so overall space complexity will be O(N ^ 2).

Code Solution
(100% EXP penalty)
Void of Diamond
Full screen
Console