Ninja wants to build a star pattern for a given odd number.
The pattern for ‘N’ = 7 will look like this:
*
**
***
****
***
**
*
Your task is that for a given odd integer 'N', print the pattern.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains one integer, ‘N’, denoting the number of rows.
Output Format:
For each test case, print 'N' strings denoting the pattern.
The output of each test case will be printed on a separate line.
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 99
Time Limit: 1 sec
2
3
5
*
**
*
*
**
***
**
*
Test case 1:
In the first test case of sample input 1, we need to print a triangle-like pattern wherein each line, the number of stars will be increasing till the central row and then it starts decreasing. So for n=3, till 2nd-row stars increase and then start decreasing.
1
1
*
Test case 1:
As ‘N’ is equal to ‘1’, we just need to print one line.
Observe the row number carefully.
Here we can Initialising precompute a string of size (n + 1) / 2. For each row using the substring function, we can extract the number of stars needed and put them in an array of strings. We can run a loop where till the central row, we will increase the number of stars. We can keep a counter variable that will be increasing till the central row and decrease henceforth. Using this variable, we can calculate how many stars are required to be taken from the precomputed string.
O(N ^ 2) where N is the given input.
We are traversing ‘N’ rows in each test case. In each row, we are finding substring of a string that takes O(N) time complexity. Hence the overall time complexity is O(N ^ 2).
O(N ^ 2) where N is the given input.
As we are returning an array of size ‘N’ containing ‘N string”’ each of size beginning from ‘1’ to ‘(N+1)/2’ and then decreasing back to ‘1.