Ninja was very fond of patterns. For a given integer ‘N’, he wants to make the N-Star Triangle.
Example:Input: ‘N’ = 3
Output:
*
***
*****
The first and only line of each test case contains an integer ‘N’.
Output format:
Return a 2D array of characters that represents the N-Star Pattern.
1 <= N <= 20
Time Limit: 1 sec
3
*
***
*****
The first row contains two spaces, followed by a star.
The second row contains one space, followed by three stars.
The third row contains five stars.
1
*
Iterate to all the cells .
Approach:
The solution is iterating on the pattern and printing every cell with ‘*’ or spaces.
The steps are as follows :
Function void nStarTriangle(int ‘N’)
O( N * N ), Where N is the given input integer.
Two nested loops are running N*(2*N-1) times so that time complexity would be the order of NxN.
Hence the time complexity is O( N * N ).
O(1)
Since we are using constant extra space.