Pattern: Triangle of numbers

Easy
0/40
Average time to solve is 15m
profile
Contributed by
25 upvotes
Asked in companies
CognizantInfo Edge India (Naukri.com)Impelsys

Problem statement

Ninja is given a pattern. Now he is asked to print the same pattern for any given ‘N’ number of rows.

For example, Pattern for ‘N’ = 4 will be.
   1
  232
 34545
4567654
Detailed explanation ( Input/output format, Notes, Images )

Input Format:

The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains a single integer ‘N’ denoting the number of rows for which pattern needs to be printed.

Output Format:

The output of each test case will be 'N' strings denoting the pattern printed for the given ‘N’ number of rows.

Output of each test case will be printed on a separate line.

Constraints:

1 <=T <= 5 
1 <= N <= 100

Where ‘T’ is the total number of test cases and ‘N’ is the given number of rows for which pattern needs to be printed.

Time limit: 1 sec

Sample Input1 :

1 
5

Sample Output1 :

    1
   232
  34543
 4567654
567898765

Explanation of Sample Input 1:

For test case 1:
We print the given pattern for the given 5 rows where each row prints spaces initially for each row decrementing for each consecutive row and printing the values corresponding to each row according to the given pattern.

Sample Input2 :

1
4

Sample Output2 :

   1
  232
 34545
4567654

Explanation of Sample Input 2:

For test case 1:
We print the given pattern for the given 4 rows where each row prints spaces initially for each row decrementing for each consecutive row and printing the values corresponding to each row according to the given pattern.
Hint

Try to make the relationship between the number of columns and spaces in a particular row.

Approaches (1)
Observation

We can observe that there is an ‘N’ number of rows where ‘N’ is the total number of rows to be printed. And each row exactly contains ‘N’ - ‘row’ number of spaces where ‘row’ denotes the row number starting from topmost as 1. Also, when we go left to right the value of each element in a particular row increases by one starting from the ‘row’ number and then decrements in the same column from 2* ‘row’ - 1 till 'row'.

 

 Algorithm:

 

  • Declare an array of strings say ‘result’ for storing the pattern.
  • Declare a variable ‘row’ for keeping track of the number of rows
  • Run an outer loop from ‘row’ = ‘1 to ‘row’ = ‘N’.
    • Declare an empty string for storing the values in the current row.
    • Run an inner loop for printing spaces from ‘space’ = 1 to ‘space’ = ‘N’ - ‘row’ (where ‘row’ is the current row number).
    • Initialize a variable say ‘val’ = ‘row’.
    • Run an inner loop from ‘currCol’ = 1 to ‘currCol’ = ‘row' to printing values in increasing order.
      • Append ‘val’ to the current string. and increment ‘val’ by 1.
    • Set ‘val’ = 2 * (‘row’ - 1)
    • Run an inner loop from ‘currCol’ = ‘val’ to ‘currCol’ = ‘row' to print values in decreasing order.
      • Append ‘currCol’ to the current string.
    • Add current string into ‘result’.
  • Return ‘result’.
Time Complexity

O(N ^ 2), where ‘N’ is the given number of rows.

 

As we will be traversing the given number of rows and for each row number, we will be traversing it from 1 to that row number for printing spaces, and traversing 1 to row number, and 2* row -1 to row for printing the columns of the pattern, Hence the overall time complexity will be O(N ^ 2).

Space Complexity

O(N ^ 2), where ‘N’ is the given number of rows.

 

As we are using an array of strings that takes O(N ^ 2) space.

Code Solution
(100% EXP penalty)
Pattern: Triangle of numbers
Full screen
Console