Crazy Numbers

Easy
0/40
Average time to solve is 10m
47 upvotes
Asked in companies
HCL TechnologiesNagarro SoftwareKellton Tech Solutions Limited

Problem statement

Ninja wants to create a right-aligned triangle of numbers across N rows. The first row contains 1 number, the second contains 2, and so on. The numbers increase sequentially starting from 1 and wrap back to 1 after 9.

You are given an integer ‘N’ denoting the given number of rows. Can you print the pattern that Ninja wants to create?

Pattern for N = 4:

   1
  23
 456
7891
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains ‘T’, denoting the number of test cases.

Each test case contains a single integer ‘N’, denoting the number of rows.
Output Format:
For each test case, print the integers denoting the required pattern in the following ‘N’ lines.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 500

Time Limit: 1 sec
Sample Input 1 :
2
3
4
Sample Output 1 :
  1
 23
456
   1
  23
 456
7891
Explanation for Sample Input 1 :
In the first test case, we are required to create a pattern consisting of 3 lines. First-line contains ‘1’. From the second line, we have to place a number one more than the previous value. So we place 2.Then we put 3 and move to the following line because this line will contain only 2 elements. In the following line, we have to place 3 numbers, so we put 4, 5, and 6.

 In the second test case, we are required to create a pattern consisting of 4 lines. First-line contains ‘1’. From the second line, we have to place a number one more than the previous value. So we place 2 elements. We put 2 and 3 and move to the following line because this line will contain only 2 elements. In the following line, we have to place 3 numbers, so we put 4, 5, and 6. In the last line, we have to place 4 digits, so we place 7, 8, 9, and 1.
Sample Input 2 :
2
7
2
Sample Output 2 :
      1
     23
    456
   7891
  23456
 789123
4567891
 1
23
Hint

Can you print one line at a time?

Approaches (1)
Brute force

The key here is to traverse all the lines sequentially, and for each line, we print the required character at the given index.

The steps are as follows:

  • We will define matrix ‘ans’ to store the final pattern.
  • We will initialize a variable ‘k’ to 1, which will be the starting value for the pattern.
  • We will iterate over all the rows, i.e., i = 0 to i = N - 1:
    • We will run a for loop starting from j = N - i -1 with the condition that j is less than or equal to N:
      • If k is greater than 9, we will insert the value in the matrix ‘ans’.
      • Otherwise, we will reset the value of k to 1 and then insert the value in the matrix ‘ans’.
  • We will return the matrix ‘ans’ as the final answer.
Time Complexity

O(N^2), where N is the given integer.
 

As we are printing every character of each line one at a time. The first line contains one character, the second line two, and so on. Hence the time required to print N lines is 1 + 2 + 3 + … + N = N * (N+1)/2, which is equivalent to O(N^2).

 

Hence the overall time complexity is O(N^2). 

Space Complexity

O(N^2), where N is the given integer.

 

We are using a matrix, to store the pattern. Hence, the overall space complexity is O(N^2).

Code Solution
(100% EXP penalty)
Crazy Numbers
Full screen
Console