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

N-Triangles

Easy
0/40
Average time to solve is 5m
profile
Contributed by
72 upvotes

Problem statement

Sam is making a Triangular painting for a maths project.

An N-dimensional Triangle is represented by the lower triangle of the pattern filled with integers starting from 1.

For every value of ‘N’, help sam to print the corresponding N-dimensional Triangle.

Example:
Input: ‘N’ = 3

Output: 
1
1 2 
1 2 3
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line contains an integer, ‘N’.
Output format:
Print the pattern as specified.
Constraints :
1  <= N <= 25
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
1
1 2 
1 2 3
Sample Input 2 :
1
Sample Output 2 :
1
Hint

 Iterate to all the cells.

Approaches (1)
Brute Force

Approach: 

The solution to the problem lies in just iterating to all the NxN cells of the pattern and printing every cell in the lower triangle.

The steps are as follows :

Function void nTriangle(int ‘N’)

  1. For loop ‘row’ in range 0 to N-1.
    • For loop ‘col’ in range 0 to ‘row’.
      •   print ‘col+1’.
    • Print the next line.
Time Complexity

O(N*N), Where N is the dimension of the square matrix. 

There are two nested loops, so time complexity would be the order of NxN.

Hence the time complexity is O(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)
N-Triangles
All tags
Sort by
Search icon

Interview problems

Cpp easy approach

void nTriangle(int n) {
	// Write your code 
	for(int i = 1; i <= n; i++){
        for (int j = 1; j < i; j++){
            cout << j << " "; 
        }
        cout << i << endl;
    }
}

beginners

4 views
0 replies
0 upvotes

Interview problems

cpp solution

void nTriangle(int n) {
	// Write your code here
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		{
			cout<<j<<" ";
		}
		cout<<endl;
	}
}
42 views
0 replies
0 upvotes

Interview problems

C++ Solution

for (int i=1; i<=n; i++){

        for(int j=1; j<=i; j++){

            cout << j << " ";

        }

        cout << endl;

    }

}

45 views
0 replies
0 upvotes

Interview problems

Pattern

void nTriangle(int n) {

    // Write your code here

    for(int i=1;i<=n;i++){

        for(int j=1;j<=i;j++){

            cout<<j<<" ";

        }

        cout<<endl;

    }

}

10 views
0 replies
0 upvotes

Interview problems

solution in cpp

void nTriangle(int n) {

    // Write your code here

    for(int i =1; i<=n ; i++){

        for(int j =1; j<=i; j++){

            cout<<j<< " ";

        }

        cout<<endl;

    }

}

19 views
0 replies
0 upvotes

Interview problems

N-Triangles

def nTriangle(n:int) ->None:

    # Write your solution here.

    for i in range(n):

        for j in range(i+1):

            print(j+1,end=" ")

        print()

    pass

28 views
0 replies
0 upvotes

Interview problems

N-Triangles Solution in Java

public class Solution {
    public static void nTriangle(int n) {
        // Write your code here
        for(int i=1; i<=n; i++){
            for(int j=1; j<=i; j++){
                System.out.print(j+" ");
            }
            System.out.println();
        }
    }
}

java

beginners

programming

Coding Ninjas

+1 more
67 views
0 replies
0 upvotes

Interview problems

Best way to solve this problem

void nTriangle(int n) {

    // Write your code here

    

    for(int i =0 ; i<n;i++){

        int count = 1;

        for(int j=0;j<n;j++){

            if(i>=j){

                cout<<count<<" ";

                count++;

            }

            else {

                cout<<" ";

            }

        }

        cout<<endl;

    }

}

43 views
0 replies
3 upvotes
profile

Interview problems

Python Sol using for loop

def nTriangle(n:int) ->None:

    # Write your solution here.

    for i in range(1,n+1):

        for j in range(1,i+1):

            print(j,end=" ")

        print()

    pass

65 views
0 replies
1 upvote

Interview problems

Number triangle pattern solution in java

import java.util.Scanner;

 

public class Solution {

    public static void nTriangle(int n) {

        // Write your code here

        for(int i=1;i<=n;i++)

        {

            for(int j=1;j<=i;j++)

            {

                System.out.print(j+" ");

                

            }

            System.out.println();

        }

    }

}

56 views
0 replies
0 upvotes
Full screen
Console