You have been given an integer ‘N’. You need to print an inverted isosceles triangle of stars such that the height of the triangle is N.
For example :If N = 4 then the output will
*******
*****
***
*
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains an integer ‘N’ which denotes the height of the inverted isosceles triangle of stars to be printed.
Output Format:
For each test case, print an inverted isosceles triangle of stars such that the height of the triangle is N
Print the output of each test case in a separate line.
1 <= T <= 100
1 <= N <= 100
Time Limit : 1 sec
1
4
*******
*****
***
*
1
3
*****
***
*
Can you think of running loops to make the triangle inverted and isosceles?
The basic idea of this approach is to run two loops nested inside an outer loop. The inner two loops make the triangle isosceles, while the outer loop decides the height of the triangle. We can observe the number of stars required at any height ‘h’ is (2 * h - 1).
O(N ^ 2), where ‘N’ is the height of the triangle.
Since we are running two loops. One decides the height of the tree and the other one makes the triangle isosceles. So the overall time complexity will be O(N ^ 2)
O(1)
Constant extra space is required.