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

Alpha-Triangle

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

Problem statement

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
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line 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:
C
C B 
C B A
Sample Input 2 :
1
Sample Output 2 :
A
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 with the required character.

The steps are as follows :

Function (void) alphaTriangle(int ‘N’)

  • For loop ‘row’ in range 0 to N-1.
    • For loop ‘col’ in range 0 to ‘row’.
      •   Print ('A' + N - col - 1).
    • 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-Triangle
All tags
Sort by
Search icon

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;

    }

}

63 views
0 replies
0 upvotes

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

programming

17 views
0 replies
0 upvotes

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

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

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;

    }

}

17 views
0 replies
1 upvote

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

        }

    }

}

93 views
0 replies
1 upvote

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

java

42 views
0 replies
0 upvotes

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;

    }

}

74 views
1 reply
3 upvotes

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;

  }

}

71 views
0 replies
1 upvote

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

        }

    }

}

45 views
0 replies
0 upvotes
Full screen
Console