void nNumberTriangle(int n) {
for(int i=n;i>0;i--){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
cout<<endl;
}
}
Problem of the day
Aryan and his friends are very fond of the pattern. For a given integer ‘N’, they want to make the Reverse N-Number Triangle.
Example:Input: ‘N’ = 3
Output:
1 2 3
1 2
1
The first line of the input contains an integer 'N’.
Output format:
Print the output as specified.
1 <= N <= 20
Time Limit: 1 sec
3
1 2 3
1 2
1
4
1 2 3 4
1 2 3
1 2
1
7
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Iterate to all the cells.
Approach:
The solution is iterating on the pattern and printing every cell with the required number.
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.
O(1)
Since we are using constant extra space.
Hence the space complexity is O(1).
Interview problems
Reverse Number Triangle Problem Solution in CPP
void nNumberTriangle(int n) {
for(int i=n;i>0;i--){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
cout<<endl;
}
}
Interview problems
cpp solution
void nNumberTriangle(int n) {
// Write your code here.
for (int i=0;i<=n;i++)
{
for (int j = 1; j < n - i + 1; j++) {
cout << j << " ";
}
cout<<endl;
}
}Interview problems
c++ code
void nNumberTriangle(int n) {
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i+1;j++){
cout<<j<<" ";
}
cout<<endl;
}
}Interview problems
c++ code
void nNumberTriangle(int n) {
for(int i=0;i<n;i++){
for(int j=1;j<=n-i;j++){
cout<<j<<" ";
}
cout<<endl;
}
}Interview problems
Pattern
void nNumberTriangle(int n) {
// Write your code here.
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i+1;j++){
cout<<j <<" ";
}
cout<<endl;
}
}
Interview problems
sol in cpp
void nNumberTriangle(int n) {
// Write your code here.
for(int i = 0; i<n; i++){
for(int j=1; j<=(n-i); j++){
cout<<j<<" ";
}
cout<<endl;
}
}
Interview problems
Reverse Number Triangle
def nNumberTriangle(n: int) -> None:
# Write your solution here.
for i in range(n,0,-1):
for j in range(i):
print(j+1,end=" ")
print()
pass
Interview problems
Java Code
public class Solution {
public static void nNumberTriangle(int n) {
// Write your code here
for(int i=1;i<=n;i++){
for(int j=1;j<=(n-i+1);j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
Interview problems
Reverse Number Triangle Solution in Java
public class Solution {
public static void nNumberTriangle(int n) {
// Write your code here
for(int i=n; i>=1; i--){
for(int j=1; j<=i; j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}Interview problems
cpp easy solutin
void nNumberTriangle(int n) {
// Write your code here.
for(int i=n;i>0;i--){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
cout<<endl;
}
}