public class Solution {
public static void alphaRamp(int n) {
for (int i = 0; i < n; i++) {
char ch = (char) ('A' + i);
for (int j = 0; j <= i; j++) {
System.out.print(ch + " ");
}
System.out.println();
}
}
}
Problem of the day
Sam is curious about Alpha-Ramp, so he decided to create Alpha-Ramp of different sizes.
An Alpha-Ramp is represented by a triangle, where alphabets are filled from the top in order.
For every value of ‘N’, help sam to return the corresponding Alpha-Ramp.
Example:Input: ‘N’ = 3
Output:
A
B B
C C C
The first and only line contains an integer, ‘N’.
Output format:
Print the pattern as specified.
1 <= N <= 25
Time Limit: 1 sec
3
A
B B
C C C
1
A
4
A
B B
C C C
D D D D
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.
The steps are as follows :
Function (void) aphaRamp(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
Using Java
public class Solution {
public static void alphaRamp(int n) {
for (int i = 0; i < n; i++) {
char ch = (char) ('A' + i);
for (int j = 0; j <= i; j++) {
System.out.print(ch + " ");
}
System.out.println();
}
}
}
Interview problems
ASCII CODE || Python
def alphaRamp(n: int) -> None:
base = 65
for i in range(1,n+1):
for j in range(1,i+1):
print(chr(base),end=" ")
base +=1
print()
pass
Interview problems
c++ code
void alphaRamp(int n) {
char ch = 'A';
for(char i=0;i<n;i++){
for(int j=0;j<=i;j++){
cout<<ch<<" ";
}
cout<<endl;
ch++;
}
}Interview problems
c++ code
void alphaRamp(int n) {
for(char i=0;i<n;i++){
char ch = 'A' +i;
for(int j=0;j<=i;j++){
cout<<ch<<" ";
}
cout<<endl;
}
}
Interview problems
alphaRamp
void alphaRamp(int n) {
// Write your code here.
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<char(64+i)<<" ";
}
cout<<endl;
}
}
Interview problems
C++ logic
void alphaRamp(int n) {
// Write your code here.
for(int i=0;i<n;i++){
char v='A'+i;
for(int ch=0;ch<=i;ch++){
cout<<v<<" ";
}
cout<<endl;
}
}
Interview problems
java Code
public class Solution {
public static void alphaRamp(int n) {
// Write your code here
for(int i=0;i<n;i++){
char ch = (char)('A' + i);
for(int j =0;j<=i;j++){
System.out.print(ch+" ");
}
System.out.println();
}
}
}
Interview problems
Alpha-Ramp
def alphaRamp(n: int) -> None:
# Write your solution from here.
for i in range(n):
for j in range(i+1):
print(chr(ord('A')+i),end=" ")
print()
pass
Interview problems
java code
public class Solution {
public static void alphaRamp(int n) {
// Write your code here
int count =64;
for(int i=1;i<=n;i++){
count++;
for(int j=1;j<=i;j++){
System.out.print((char)count+" ");
}
System.out.println();
}
}
}
Interview problems
Alpha-Ramp
void alphaRamp(int n) {
for(int i = 0; i<n; i++){
char ch = 'A'+i;
for(int j = 0; j<=i; j++){
cout<<ch<<" ";
}
cout<<endl;
}
}