Make alphabets using the elements of an array

Given an array of positive integers, find all the alphabets that we can make using elements of the array.

Examples :

Input : arr[] = {6, 5}
Output : A
In this example, we can take 6 and 5
as 65 that gives us A.

Input : arr[] = {5, 6, 6}
Output : A B
Here we can take 6 and 5 as 65 to make A. 
And 6 and 6 as 66 to make B.

Input : arr[] = {1, 0, 1, 0}
Output: d e n
Here we can take 1, 0 and 0 as 100 to make d.
1, 0 and 1 as 101 to make e. And 
1, 1, and 0 as 110 to make n.



Approach :
The approach is to iterate a loop from char A to Z and check whether both the digits of their ASCII values (ASCII value of A is 65 and digits of ASCII value for A is 6 and 5) exist in the array or not. For this we have to calculate frequency of each element of the array. Same as for small alphabets.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find all the capital characters
// we can make from the given array.
#include <bits/stdc++.h>
using namespace std;
  
// Function returns all the capital characters
//  we can make.
string findCharacters(int arr[], int n)
{
    string ans = "";
    int brr[10] = { 0 };
  
    // Calculating the frequency of the elements.
    for (int i = 0; i < n; i++)
        brr[arr[i]]++;
  
    // Checking for capital alphabets.
    for (char ch = 'A'; ch <= 'Z'; ch++)
    {
  
        // Storing one digit in x and other in y.
        int x = ch / 10;
        int y = ch % 10;
        brr[x]--;
        brr[y]--;
  
        // If both the digits exist.
        if (brr[x] >= 0 && brr[y] >= 0)
        {
  
            // Putting in the result
            ans += ch;
            ans += ' ';
        }
        brr[x]++;
        brr[y]++;
    }
  
    // Checking for alphabets a, b and c.
    for (char ch = 'a'; ch <= 'c'; ch++)
    {
        int x = ch / 10;
        int y = ch % 10;
        brr[x]--;
        brr[y]--;
  
        // If all the digits exist.
        if (brr[x] >= 0 && brr[y] >= 0)
        {
  
            // Putting in the result
            ans += ch;
            ans += ' ';
        }
        brr[x]++;
        brr[y]++;
    }
  
    // Checking for d to z.
    for (char ch = 'd'; ch <= 'z'; ch++) 
    {
        int x = (ch / 10) / 10;
        int y = (ch / 10) % 10;
        int z = ch % 10;
        brr[x]--;
        brr[y]--;
        brr[z]--;
  
        // If all the digits exist.
        if (brr[x] >= 0 && brr[y] >= 0 && brr[z] >= 0)
        {
  
            // Putting in the result
            ans += ch;
            ans += ' ';
        }
        brr[x]++;
        brr[y]++;
        brr[z]++;
    }
  
    return ans;
}
  
// Driver function
int main()
{
    int arr[] = { 5, 6, 6 }, n;
    n = sizeof(arr) / sizeof(arr[0]);
    cout << findCharacters(arr, n) << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find all the capital characters
// we can make from the given array.
import java.util.*;
import java.lang.*;
  
public class GeeksforGeeks
{
  
    // Function returns all the capital characters we can make.
    public static String findCharacters(int arr[], int n)
    {
        String ans = "";
        int[] brr = new int[10];
  
        // Calculating the frequency of the elements.
        for (int i = 0; i < n; i++)
            brr[arr[i]]++;
  
        for (char ch = 'A'; ch <= 'Z'; ch++)
        {
  
            // Storing one digit in x and other in y.
            int x = ch / 10;
            int y = ch % 10;
            brr[x]--;
            brr[y]--;
  
            // If both the digits exist.
            if (brr[x] >= 0 && brr[y] >= 0)
            {
  
                // Putting in the result
                ans += ch;
                ans += ' ';
            }
            brr[x]++;
            brr[y]++;
        }
  
        // Checking for alphabets a, b and c.
        for (char ch = 'a'; ch <= 'c'; ch++)
        {
            int x = ch / 10;
            int y = ch % 10;
            brr[x]--;
            brr[y]--;
  
            // If all the digits exist.
            if (brr[x] >= 0 && brr[y] >= 0)
            {
  
                // Putting in the result
                ans += ch;
                ans += ' ';
            }
            brr[x]++;
            brr[y]++;
        }
  
        // Checking for d to z.
        for (char ch = 'd'; ch <= 'z'; ch++)
        {
            int x = (ch / 10) / 10;
            int y = (ch / 10) % 10;
            int z = ch % 10;
            brr[x]--;
            brr[y]--;
            brr[z]--;
  
            // If all the digits exist.
            if (brr[x] >= 0 && brr[y] >= 0 && brr[z] >= 0)
            {
  
                // Putting in the result
                ans += ch;
                ans += ' ';
            }
            brr[x]++;
            brr[y]++;
            brr[z]++;
        }
  
        return ans;
    }
  
    // Driver function
    public static void main(String argc[])
    {
        int arr[] = { 5, 6, 6 }, n;
        n = 3;
        System.out.println(findCharacters(arr, n));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find all the capital
// characters we can make from the given array.
using System;
  
class GeeksforGeeks
{
    // Function returns all the capital
    // characters we can make.
    public static String findCharacters(int []arr, int n )
    {
        String ans = "";
        int []brr = new int[10];
  
        // Calculating the frequency of the elements.
        for (int i = 0; i < n; i++)
            brr[arr[i]]++;
  
        for (char ch = 'A'; ch <= 'Z'; ch++)
        {
            // Storing one digit in x and other in y.
            int x = ch / 10;
            int y = ch % 10;
            brr[x]--;
            brr[y]--;
  
            // If both the digits exist.
            if (brr[x] >= 0 && brr[y] >= 0)
            {
                // Putting in the result
                ans += ch;
                ans += ' ';
            }
            brr[x]++;
            brr[y]++;
        }
  
        // Checking for alphabets a, b and c.
        for (char ch = 'a'; ch <= 'c'; ch++)
        {
            int x = ch / 10;
            int y = ch % 10;
            brr[x]--;
            brr[y]--;
  
            // If all the digits exist.
            if (brr[x] >= 0 && brr[y] >= 0)
            {
  
                // Putting in the result
                ans += ch;
                ans += ' ';
            }
            brr[x]++;
            brr[y]++;
        }
  
        // Checking for d to z.
        for (char ch = 'd'; ch <= 'z'; ch++)
        {
            int x = (ch / 10) / 10;
            int y = (ch / 10) % 10;
            int z = ch % 10;
            brr[x]--;
            brr[y]--;
            brr[z]--;
  
            // If all the digits exist.
            if (brr[x] >= 0 && brr[y] >= 0 && brr[z] >= 0)
            {
                // Putting in the result
                ans += ch;
                ans += ' ';
            }
            brr[x]++;
            brr[y]++;
            brr[z]++;
        }
  
        return ans;
    }
  
    // Driver function
    public static void Main()
    {
        int []arr = {5, 6, 6};
        int n = 3;
        Console.Write(findCharacters(arr, n));
    }
}
  
// This code is contributed by nitin mittal.

chevron_right



Output:

A B

Time Complexity: O(n)



My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nitin mittal, gfg_sal_gfg



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.