Array ( [0] => [1] => questions [2] => Basic [3] => Alpha-Triangle ) Patterns | Alpha Triangle | THE INQUISITIVE





Alpha Triangle

LEVEL:Beginner

Description

Print the pattern as shown below for all test cases.

Input Format

First line contains the value N.

Output Format

Print the output as shown in below examples.


Example 1:

Input
3
Output
*         *
* *     * *
* * * * * *
* *     * *
*         *
Example 2:

Input
4
Output
*             *
* *         * *
* * *     * * *
* * * * * * * *
* * *     * * *
* *         * *
*             *
Example 3:

Input
5
Output
*                 *
* *             * *
* * *         * * *
* * * *     * * * *
* * * * * * * * * *
* * * *     * * * *
* * *         * * *
* *             * *
*                 *

oops

Login to see Discussion




Approach


This problem can be solved into two sub problems
one part prints the upper part and other prints the lower part

Step-i)assign 1 to i
Step-ii)assign 1 to j
Step-iii) if j is less than or equal to i or j is greater than equal to 2*N-i+1 print * and space (" ")
Step-iv)else print only two spaces (" ")
Step-v)increment j by one value
Step-vi)repeat steps iii to v till j is less than or equal to 2*N
Step-vii)go to next line in printing
Step-viii)increment i value by one
Step-ix)repeat steps iii to viii till i is less than or equal to N(By now we have printed upper part of the pattern)
Step-x)assign N to i
Step-xi)assign 1 to j
Step-xii)if j is less than or equal to i or j is greater than or equal to 2*N-i+1 print * and space (" ")
Step-xiii)else print two spaces (" ")
Step-xiv)increment j value by one
Step-xv)repeat steps xii to xiv till j is less than or equal to 2*N-i+1
Step-xvi)go to next line and decrement i value by one
Step-xvii) repeat steps from xi to xvi till is greater than or equal to 1

Time Complexity:- O(n^2)
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution