Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Reverse Number Triangle

Easy
0/40
profile
Contributed by
81 upvotes

Problem statement

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
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer 'N’.
Output format:
Print the output as specified.
Constraints :
1  <= N <= 20
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
1 2 3
1 2
1
Sample Input 2 :
4
Sample Output 2 :
1 2 3 4
1 2 3
1 2
1
Sample Input 3 :
7
Sample Output 3 :
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
Hint

Iterate to all the cells.

Approaches (1)
Brute Force

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’)

 

  1. Int ‘num’ = ‘N’.
  2. For ‘i’ from 0 to ‘N’-1:
    • For ‘j’ from 1 to ‘num’:
      • Print ‘j’.
    • Decrease ‘num’ by 1
    • End the current line.
Time Complexity

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.

 

Space Complexity

O(1)

Since we are using constant extra space.

Hence the space complexity is O(1).

Code Solution
(100% EXP penalty)
Reverse Number Triangle
All tags
Sort by
Search icon

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;

    }

}

27 views
0 replies
0 upvotes

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;
    }

}
27 views
0 replies
0 upvotes

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;
    }
}
6 views
0 replies
0 upvotes

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;
    }
}
5 views
0 replies
0 upvotes

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;

    }

}

17 views
0 replies
0 upvotes

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;

    }

}

8 views
0 replies
0 upvotes

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

14 views
0 replies
0 upvotes

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();

    }

}

}

38 views
0 replies
0 upvotes

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();
        }
    }
}

java

beginners

programming

Coding Ninjas

+1 more
19 views
0 replies
0 upvotes

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;

        }

        

    }

38 views
0 replies
0 upvotes
Full screen
Console