void alphaTriangle(int n) {
// Write your code here.
for(int i=1; i<=n; i++){
char ch= 'A'+ (n-1);
for(int j=1;j<=i; j++){
cout<<ch<<" ";
ch--;
}
cout<<endl;
}
}
Problem of the day
Sam is researching on Alpha-Triangles. So, he needs to create them for different integers ‘N’.
An Alpha-Triangle is represented by the triangular pattern of alphabets in reverse order.
For every value of ‘N’, help sam to print the corresponding Alpha-Triangle.
Example:Input: ‘N’ = 3
Output:
C
C B
C B A
The first line and only line contains an integer, ‘N’.
Output format:
Print the pattern as specified.
1 <= N <= 25
Time Limit: 1 sec
3
C
C B
C B A
1
A
Iterate to all the cells.
Approach:
The solution to the problem lies in just iterating to all the N*N cells of the pattern and filling every cell with the required character.
The steps are as follows :
Function (void) alphaTriangle(int ‘N’)
O(N*N)
There are two nested loops, so 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
took me 12 mins
void alphaTriangle(int n) {
// Write your code here.
for(int i=1; i<=n; i++){
char ch= 'A'+ (n-1);
for(int j=1;j<=i; j++){
cout<<ch<<" ";
ch--;
}
cout<<endl;
}
}
Interview problems
Reverse Iteration || Python
def alphaTriangle(n: int):
base = 65+n
for i in range(1,n+1):
for j in range(1,i+1):
print(chr(base-j),end=" ")
print()
pass
Interview problems
c++ code
// code for striver problem
void alphaTriangle(int n) {
for(int i=0;i<n;i++){
for(char ch='E'-i;ch<'E';ch++){
cout<<ch<<" ";
}
cout<<endl;
}
}Interview problems
c++ code
void alphaTriangle(int n) {
for(int i=1;i<=n;i++){
char ch='A'+n-1;
for(int j=0;j<i;j++){
cout<<char(ch-j)<<" ";
}
cout<<endl;
}
}Interview problems
solution
void alphaTriangle(int n) {
// Write your code here.
for(int i=0;i<n;i++){
for(int j=1;j<=i+1;j++){
cout<<char(64+1+n-j)<<" ";
}
cout<<endl;
}
}
Interview problems
java code
public class Solution {
public static void alphaTriangle(int n) {
// Write your code here
for(int i=1;i<=n;i++){
char ch=(char)('A'+n);
for(int j=1;j<=i;j++){
ch--;
System.out.print(ch+" ");
}System.out.println();
}
}
}
Interview problems
Alpha Triangle very simple JAVA solution
public static void alphaTriangle(int n) {
// Write your code here
for(int row=1; row<=n; row++){
char val = (char)('A' + n);
for(int col=1; col<=row; col++){
val = (char)(val-1);
System.out.print(val + " ");
}
System.out.println();
}
}Interview problems
c++
void alphaTriangle(int n) {
// Write your code here.
for(int i=0;i<n;i++)
{
int k=65+n-1;
for(int j=0;j<i+1;j++)
{
cout<<char(k)<<" ";
k--;
}
cout<<endl;
}
}
Interview problems
Alpha-Triangle
void alphaTriangle(int n) {
for(int i = 1; i<=n; i++){
char ch = 'A';
for(int j = 1; j<=i; j++){
cout<<char(ch+n-j)<<" ";
}
cout<<endl;
}
}
Interview problems
Alpha Triangle
public class Solution {
public static void alphaTriangle(int n) {
// Write your code here
char ch;
for(int i = 0; i<n;i++)
{
ch='A';
ch+=(n-1);
for(int j =0;j<=i;j++)
{
System.out.print(ch+" ");
ch--;
}
System.out.println();
}
}
}