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

Reverse Letter Triangle

Easy
0/40
profile
Contributed by
42 upvotes

Problem statement

Aryan and his friends are very fond of patterns. For a given integer ‘N’, they want to make the Reverse Letter Triangle.

You must print a matrix corresponding to the given Reverse Letter Triangle.

Example:
Input: ‘N’ = 3

Output: 

A B C
A B
A
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer ‘N’.
Output format:
Print a 2D array of characters that represents the Reverse Letter Triangle.
Constraints :
1  <= N <= 20
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
A B C
A B
A
Sample Input 2 :
4
Sample Output 2 :
A B C D
A B C
A B
A
Sample Input 3 :
7
Sample Output 3 :
A B C D E F G 
A B C D E F 
A B C D E 
A B C D 
A B C 
A B 
A 
Hint

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

Approaches (1)
Brute Force

Approach: 

The steps are as follows :

Function (void) nLetterTriangle(int ‘N’)

 

  1. Int ‘num’ = ‘N’
  2. for ‘i’ from 0 to ‘N’-1:
    1. char ‘ch’=’A’.
    2. for j from 0 to ‘num’-1:
      • Print ‘ch’
      • ch++
    3. End the current line.
    4. ‘num’--
Time Complexity

O( N * N )

 

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)
Reverse Letter Triangle
All tags
Sort by
Search icon

Interview problems

cpp sol

void nLetterTriangle(int n) {

    // Write your code here.

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

        for(char ch='A'; ch<'A' + (n-i); ch++){

            cout<<ch<<" ";

            

        }

        cout<<endl;

    }

}

17 views
0 replies
0 upvotes

Interview problems

Using ASCII || Python3

def nLetterTriangle(n: int):
    base = 64
    for i in range(n,0,-1):
        for j in range(1,i+1):
            print(chr(base+j),end=" ")
        print()
    pass

python

beginners

7 views
0 replies
0 upvotes

Interview problems

c++ code

void nLetterTriangle(int n) {
    for(int i=0;i<n;i++){
        char ch = 'A';
        for(int j=0;j<n-i;j++){
            cout<<ch<<" ";
            ch++;
        }
        cout<<endl;
    }
}
15 views
0 replies
0 upvotes

Interview problems

nletterTriangle

void nLetterTriangle(int n) {

    // Write your code here.

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

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

            cout<<char(65+j)<<" ";

        }

        cout<<endl;

    }

}

3 views
0 replies
0 upvotes

Interview problems

Java Code

public class Solution {

    public static void nLetterTriangle(int n) {

        // Write your code here

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

            for(char ch='A';ch<='A'+(n-i-1);ch++){

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

            }

            System.out.println();

 

        }

    }

}

 

java

25 views
0 replies
0 upvotes

Interview problems

Reverse Letter Triangle

def nLetterTriangle(n: int):

    # Write your solution here.

    for i in range(n,0,-1):

        for j in range(i):

            print(chr(ord('A')+j),end=" ")

        print()

    pass

 

4 views
0 replies
0 upvotes

Interview problems

java

 

public class Solution {

    public static void nLetterTriangle(int n) {

        // Write your code here

        int x=n;

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

            char start ='A';

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

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

                start++;

 

            } 

        

             x--;

            System.out.println();

        }

        }

 

        }

5 views
0 replies
0 upvotes

Interview problems

java code

public class Solution {

    public static void nLetterTriangle(int n) {

        // Write your code here

        for(int i=n;i>=1;i--){

            int count=65;

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

                System.out.print((char)count+" ");

                count++;

            }

            System.out.println();

        }

    }

}

 

9 views
0 replies
0 upvotes

Interview problems

Reverse Triangle C++ Solution

void nLetterTriangle(int n) {

    // Write your code here.

     for(int i =n;i>0;i--){

        for(char ch = 'A';ch <= 'A'+i-1;ch++){

            cout<<ch<<" ";

        }

        cout<<endl;

    }

}

16 views
0 replies
1 upvote

Interview problems

Reverse Letter Triangle

void nLetterTriangle(int n) {

    for(int i = n; i>=1; i--){

        for(char ch = 'A'; ch<'A'+i; ch++){

            cout<<ch<<" ";

        }

        cout<<endl;

    }

}

18 views
0 replies
0 upvotes
Full screen
Console