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





Upper Row Sum 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
  1
 1 1
1 2 1
Example 2:

Input
4
Output
   1
  1 1
 1 2 1
1 3 3 1
Example 3:

Input
5
Output
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

oops

Login to see Discussion




Approach


Step-i) assign 1 to k and 0 to i
Step-ii) assign 1 to space
Step-iii) print space (" ") and increment the value of space by one value
Step-iv)repeat step iii till space is less than or equal to N-i
Step-v) assign 0 to j
Step-vi) if j is equal to 0 or i is equal to 0 assign 1 to k else assign k*(i-j+1)/j to k
Step-vii)print the value of k and space (" ") and increment j by one value
Step-viii)repeat steps vi and vii till j is less than or equal to i
Step-ix)go to next line and increment the value of i by one
Step-x)repeat the steps ii to ix till i is less than N

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