Last Updated: 26 Nov, 2020

Patternify

Easy

Problem statement

You are given an integer ‘N’. Your task is to print a pattern with the following description

The pattern will consist of ‘N’ lines.

For 1 <= ‘i’ <= ‘N’ ‘i’th line consists of a string of ‘N’  + 1 - ‘i’  ‘*’ characters.

For example:

If ‘N’ is 5 then the pattern will be.

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

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:

The output for each test case will be 'N' strings denoting the pattern printed for the given ‘N’ number of rows.

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

Constraints:

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

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 first create a string of ‘N’ ‘*’ characters, and modify this string for each step. The 1st line of the pattern will always consist of ‘N’ characters, so we will first create a string of size ‘N’ and then we will decrease the length by 1 in each step till we get an empty string.

 

Algorithm:

 

  • Create a string of ‘N’ - ‘*’ characters, and an array of strings for storing the pattern.
  • Run a loop from 1 to ‘N’
  • Append current string into answer array
  • Decrease length by 1 for this we can pop the last character of the string.
  • Return the answer array that contains the pattern.