void nNumberTriangle(int n) {
// Write your code here.
int num=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<num<<" ";
num =num+1;
}
cout<<endl;
}
}
Problem of the day
Aryan and his friends are very fond of patterns. For a given integer ‘N’, they want to make the Increasing Number Triangle.
Example:Input: ‘N’ = 3
Output:
1
2 3
4 5 6
The first line of the input contains an integer 'N’.
Output format:
Print the pattern as specified.
1 <= T <= 10
1 <= N <= 20
Time Limit: 1 sec
3
1
2 3
4 5 6
4
1
2 3
4 5 6
7 8 9 10
7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
Iterate to all the cells and fill the cells in an increasing sequence.
Approach:
The steps are as follows :
Function (void) nNumberTriangle(int ‘N’)
O( N * N ), Where N is the given input integer.
Two nested loops are running N * N times so that time complexity would be the order of N * N.
Hence the time complexity is O( N * N ).
O(1)
No extra space is used as we are just printing the pattern.
Interview problems
Using C++
void nNumberTriangle(int n) {
// Write your code here.
int num=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<num<<" ";
num =num+1;
}
cout<<endl;
}
}
Interview problems
Increaing Number Triangle || Using Counter || Python
Try to relate terminating number with the line number and from there we get a relation that when terminating number = line_number*(line_number + 1)/2 using this condition now our work become easy that we just have to check for this condition each time if this holds that changes line else do noting
def nNumberTriangle(n: int) -> None:
# Write your solution here.
k = 1
for i in range(1,(int((n*(n+1))/2) + 1)):
print(i, end = " ")
if (i == int(k*(k+1)/2)):
print("\n")
k+= 1Interview problems
Using Counter || Python3
def nNumberTriangle(n: int) -> None:
counter=1
for i in range(1,n+1):
for j in range(1,i+1):
print(counter,end=" ")
counter+=1
print()
pass
Interview problems
easy peasy in cpp
void nNumberTriangle(int n) {
// Write your code here.
int number = 1;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
cout<<number<<" ";
number = number + 1;
}
cout<<endl;
}
}
Interview problems
c++ code
void nNumberTriangle(int n) {
int num=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<num<<" ";
num++;
}
cout<<endl;
}
}Interview problems
nNumberTriangle
void nNumberTriangle(int n) {
// Write your code here.
int num=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<num<<" ";
num=num+1;
}
cout<<endl;
}
}
Interview problems
increasing number triangle in c++
void nNumberTriangle(int n) {
// Write your code here.
int num = 1;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++,num++){
cout<<num<<" ";
}
cout<<endl;
}
}
Interview problems
Increasing Number Triangle
def nNumberTriangle(n: int) -> None:
# Write your solution here.
num=1;
for i in range(n):
for j in range(i+1):
print(num,end=" ")
num=num+1
print()
pass
Interview problems
Easy-to-understand Python code
def nNumberTriangle(n: int) -> None:
curr_char = 1
i = 0
while i <= n-1:
j = 0
while j <= i:
print(str(curr_char), end=' ')
curr_char += 1
j += 1
print()
i += 1
Interview problems
java code
public class Solution {
public static void nNumberTriangle(int n) {
// Write your code here
int count=1;
for(int i=1;i<=n;i++){
for(int j =1;j<=i;j++){
System.out.print(count+" ");
count++;
}System.out.println();
}
}
}