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

Alpha-Ramp

Easy
0/40
Average time to solve is 5m
profile
Contributed by
35 upvotes

Problem statement

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
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line contains an integer, ‘N’.
Output format:
Print the pattern as specified.
Constraints :
1  <= N <= 25
Time Limit: 1 sec
Sample Input 1:
3
Sample Output 1:
A
B B
C C C
Sample Input 2 :
1
Sample Output 2 :
A
Sample Input 3 :
4
Sample Output 3 :
A
B B
C C C
D D D D
Hint

Iterate to all the cells.

Approaches (1)
Brute Force

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’)

  • For loop 'row' in range 0 to N-1.
    • For loop 'col' in range 0 to N-1
      • Print ('A' + row)
    • End the current line.
Time Complexity

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 ). 

Space Complexity

O(1)

 

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

Code Solution
(100% EXP penalty)
Alpha-Ramp
All tags
Sort by
Search icon

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

        }

    }

}

 

1 view
0 replies
0 upvotes

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
4 views
0 replies
0 upvotes

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

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

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;

    }

}

 

5 views
0 replies
0 upvotes

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;

    }

}

 

8 views
0 replies
0 upvotes

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

        }

    }

}

30 views
0 replies
0 upvotes

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

 

6 views
0 replies
0 upvotes

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

        }

    }

}

22 views
0 replies
0 upvotes

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;

    }

}

 

37 views
0 replies
0 upvotes
Full screen
Console