Patternify

Easy
0/40
104 upvotes
Asked in company
Infosys

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.

*****
****
***
**
*
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:

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.

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 description first line will contain a string of 5 ‘*’, the second line will contain a string of 4 ‘*’, the third line will contain 3 ‘*’, the fourth line will contain 2 ‘*’ and the fifth line will contain 1 ’*’.

Test Case 2:

Given ‘N’ = 3
For 1 <= ‘i’ <= 3  , ‘i’th line contains 4 - ’i’, ‘*’ characters.

Sample Input 2:

2
1
2

Sample Output 2:

*
**
* 

Explanation of Sample Input 2:

Test Case 1:

Given ‘N' = 1
There will be only 1 line and that will contain a single ‘*’.

Test Case 2:

Given ‘N’ = 2
First-line will contain 2 ‘*’ while the second line will contain 1 ‘*’ as per description.
Hint

In each step, we will decrease the length of the string by 1.

Approaches (1)
Pattern printing

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.

 

Time Complexity

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

 

First, we are creating a string of ‘N’ characters it will take O(N) time and after that, we are running a loop from 1 to ‘N’ and in each step, we will append a string of size ‘i’ in the array so here time complexity of append will be O(N) and we are doing it for ‘N’ steps so overall time complexity will be 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)
Patternify
Full screen
Console