Last Updated: 27 Nov, 2020

Arrow Pattern

Easy

Problem statement

You are given an integer 'N'. Your task is to give a pattern with the following description:

1. The pattern will consist of 2 * β€˜N’ - 1 lines.
2. The pattern will consist of β€˜ ’ (space) and β€˜*’ (asterisk) characters only.
3. The pattern will be an arrow pattern.
4. For a value 'i', the arrow pattern starts with 'i' β€˜ ’ (space) characters and then has 'i' β€˜*’ (asterisk) characters.
5. The value of 'i' starts from 1, increases till β€˜N’ and then decreases till 1.


Please note that each line will consist of the same number of β€˜ ’ and β€˜*’ characters.


For a better understanding of the arrow, pattern refers to example and sample input-output.


For example:

If β€˜N’ is 5 then the pattern will be (there is a space before the first asterisk):

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

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 the 'N' strings denoting the pattern.

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


Constraints:

1 <= 'T' <= 10
1 <= 'N' <= 1000

Where β€˜T’ is the number of test cases, β€˜N’  is the size of the pattern.


Note:

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

Approaches

01 Approach

The idea is here to maintain 2 strings 1 for β€˜ β€˜ characters and 1 for β€˜*’ characters, now in each step till β€˜N’ we will append 1 β€˜ β€˜ character in 1st string and 1 β€˜*’  character in 2nd string. After β€˜N’ in each step, we will remove 1 β€˜ β€˜ character from the 1st string and 1 β€˜*’  character from the 2nd string. At each step, we will append concatenation of both strings to our pattern.

 

Algorithm:

 

  • Create 2 strings name β€˜str1’ and β€˜str2’, β€˜str1’ is for β€˜ β€˜ characters and β€˜str2’ is for β€˜*’ characters. Also, create an array of strings for storing the pattern say β€˜answer’.
  • Run a loop from β€˜i’ = 1 to β€˜i’ = 2 * β€˜N’ - 1
  • If β€˜i’ <= β€˜N’ append β€˜ β€˜ to β€˜str1’ and β€˜*’ to β€˜str2’ now append β€˜str1’ + β€˜str2’ to β€˜answer’.
  • Else, remove 1 character from β€˜str1’ and 1 character from β€˜str2’ and append β€˜str1’ + β€˜str2’ to β€˜answer’.
  • Return the β€˜answer’ array that contains the pattern.