Inverted Left Half Pyramid

Easy
0/40
0 upvote
Asked in company
Amdocs

Problem statement

You are given an integer 'n'.

Your task is to print a pattern of stars (*) and spaces () that forms an inverted, left-aligned pyramid of height 'n'.

The pattern should follow these rules:

1) The first (top) row must contain 'n' stars and 0 leading spaces. 2) Each subsequent row must contain one more leading space and one fewer star than the row above it. 3) The last (bottom) row must contain 'n-1' leading spaces and 1 star.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single integer 'n'.


Output Format:
Print 'n' lines, each containing the appropriate number of leading spaces and stars to form the pyramid.

Do not print any trailing spaces after the stars on any line.


Note:
The total number of characters (spaces + stars) on each line will always be 'n'.
Sample Input 1:
4


Sample Output 1:
****
 ***
  **
   *


Explanation for Sample 1:
For n = 4, the pyramid has 4 rows:
- Row 1: 0 spaces, 4 stars
- Row 2: 1 space, 3 stars
- Row 3: 2 spaces, 2 stars
- Row 4: 3 spaces, 1 star


Sample Input 2:
5


Sample Output 2:
*****
 ****
  ***
   **
    *


Explanation for Sample 2:
The pattern for n = 5 follows the same logic, starting with 5 stars and decreasing by one in each subsequent row while the leading spaces increase.


Expected Time Complexity:
The expected time complexity is O(n^2).


Constraints:
1 <= n <= 100

Time limit: 1sec
Approaches (1)
Inverted Left Half Pyramid
Time Complexity

The expected time complexity is O(n^2).

Space Complexity
Code Solution
(100% EXP penalty)
Inverted Left Half Pyramid
Full screen
Console