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;
}
}
Problem of the day
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
The first line contains an integer ‘N’.
Output format:
Print a 2D array of characters that represents the Reverse Letter Triangle.
1 <= N <= 20
Time Limit: 1 sec
3
A B C
A B
A
4
A B C D
A B C
A B
A
7
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
Iterate to all the cells and fill the cells in an increasing sequence of letters.
Approach:
The steps are as follows :
Function (void) nLetterTriangle(int ‘N’)
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 ).
O(1)
No extra space is used as we are just printing the pattern.
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;
}
}
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
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;
}
}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;
}
}
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();
}
}
}
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
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();
}
}
}
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();
}
}
}
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;
}
}
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;
}
}