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

Increasing Number Triangle

Easy
0/40
profile
Contributed by
39 upvotes

Problem statement

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

Iterate to all the cells and fill the cells in an increasing sequence.

Approaches (1)
Brute Force

Approach: 

 

The steps are as follows :

 

Function (void) nNumberTriangle(int ‘N’)

  1. Int ‘num’ = 1, ‘currentNum’ = 1
  2. For ‘i’ from 0 to ‘N’-1:
    • For j from 1 to ‘num’:
      • Print ‘currentNum’
      • currentNum++
    • Print an empty line
    • ‘num’++
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.

Hence the time complexity is O( N * N ). 

Space Complexity

O(1)

 

No extra space is used as we are just printing the pattern.

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

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;

    }

}

9 views
0 replies
0 upvotes

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+= 1
5 views
0 replies
0 upvotes

Interview 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

python

programming

20 views
0 replies
0 upvotes

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;

    }

}

22 views
0 replies
0 upvotes

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

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;

    }

}

6 views
0 replies
0 upvotes

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;

    }

}

13 views
0 replies
0 upvotes

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

 

11 views
0 replies
0 upvotes

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

 

 

10 views
0 replies
1 upvote

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

        }

    }

}

25 views
0 replies
0 upvotes
Full screen
Console