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++
#include <bits/stdc++.h>
using namespace std;
string findCharacters( int arr[], int n)
{
string ans = "" ;
int brr[10] = { 0 };
for ( int i = 0; i < n; i++)
brr[arr[i]]++;
for ( char ch = 'A' ; ch <= 'Z' ; ch++)
{
int x = ch / 10;
int y = ch % 10;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0)
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
for ( char ch = 'a' ; ch <= 'c' ; ch++)
{
int x = ch / 10;
int y = ch % 10;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0)
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
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 (brr[x] >= 0 && brr[y] >= 0 && brr[z] >= 0)
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
brr[z]++;
}
return ans;
}
int main()
{
int arr[] = { 5, 6, 6 }, n;
n = sizeof (arr) / sizeof (arr[0]);
cout << findCharacters(arr, n) << endl;
}
|
Java
import java.util.*;
import java.lang.*;
public class GeeksforGeeks
{
public static String findCharacters( int arr[], int n)
{
String ans = "" ;
int [] brr = new int [ 10 ];
for ( int i = 0 ; i < n; i++)
brr[arr[i]]++;
for ( char ch = 'A' ; ch <= 'Z' ; ch++)
{
int x = ch / 10 ;
int y = ch % 10 ;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0 )
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
for ( char ch = 'a' ; ch <= 'c' ; ch++)
{
int x = ch / 10 ;
int y = ch % 10 ;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0 )
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
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 (brr[x] >= 0 && brr[y] >= 0 && brr[z] >= 0 )
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
brr[z]++;
}
return ans;
}
public static void main(String argc[])
{
int arr[] = { 5 , 6 , 6 }, n;
n = 3 ;
System.out.println(findCharacters(arr, n));
}
}
|
Python
def findCharacters(arr, n):
ans = ""
brr = [ 0 ] * 10
for i in range (n):
brr[arr[i]] + = 1
for ch in range ( ord ( 'A' ), ord ( 'Z' ) + 1 ):
x = ch / / 10
y = ch % 10
brr[x] - = 1
brr[y] - = 1
if (brr[x] > = 0 and brr[y] > = 0 ):
ans + = chr (ch)
ans + = ' '
brr[x] + = 1
brr[y] + = 1
for ch in range ( ord ( 'a' ), ord ( 'c' ) + 1 ):
x = ch / / 10
y = ch % 10
brr[x] - = 1
brr[y] - = 1
if (brr[x] > = 0 and brr[y] > = 0 ):
ans + = chr (ch)
ans + = ' '
brr[x] + = 1
brr[y] + = 1
for ch in range ( ord ( 'd' ), ord ( 'z' ) + 1 ):
x = (ch / / 10 ) / / 10
y = (ch / / 10 ) % 10
z = ch % 10
brr[x] - = 1
brr[y] - = 1
brr[z] - = 1
if (brr[x] > = 0 and brr[y] > = 0 and brr[z] > = 0 ):
ans + = chr (ch)
ans + = ' '
brr[x] + = 1
brr[y] + = 1
brr[z] + = 1
return ans
arr = [ 5 , 6 , 6 ]
n = len (arr)
print (findCharacters(arr, n))
|
C#
using System;
class GeeksforGeeks
{
public static String findCharacters( int []arr, int n )
{
String ans = "" ;
int []brr = new int [10];
for ( int i = 0; i < n; i++)
brr[arr[i]]++;
for ( char ch = 'A' ; ch <= 'Z' ; ch++)
{
int x = ch / 10;
int y = ch % 10;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0)
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
for ( char ch = 'a' ; ch <= 'c' ; ch++)
{
int x = ch / 10;
int y = ch % 10;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0)
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
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 (brr[x] >= 0 && brr[y] >= 0 && brr[z] >= 0)
{
ans += ch;
ans += ' ' ;
}
brr[x]++;
brr[y]++;
brr[z]++;
}
return ans;
}
public static void Main()
{
int []arr = {5, 6, 6};
int n = 3;
Console.Write(findCharacters(arr, n));
}
}
|
Javascript
<script>
function findCharacters(arr,n)
{
let ans = "" ;
let brr = new Array(10);
for (let i=0;i<brr.length;i++)
{
brr[i]=0;
}
for (let i = 0; i < n; i++)
brr[arr[i]]++;
for (let ch = 'A' .charCodeAt(0);
ch <= 'Z' .charCodeAt(0); ch++)
{
let x = Math.floor(ch / 10);
let y = ch % 10;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0)
{
ans += String.fromCharCode(ch);
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
for (let ch = 'a' .charCodeAt(0);
ch <= 'c' .charCodeAt(0); ch++)
{
let x = Math.floor(ch / 10);
let y = ch % 10;
brr[x]--;
brr[y]--;
if (brr[x] >= 0 && brr[y] >= 0)
{
ans += String.fromCharCode(ch);
ans += ' ' ;
}
brr[x]++;
brr[y]++;
}
for (let ch = 'd' .charCodeAt(0);
ch <= 'z' .charCodeAt(0); ch++)
{
let x = Math.floor((ch / 10) / 10);
let y = (ch / 10) % 10;
let z = ch % 10;
brr[x]--;
brr[y]--;
brr[z]--;
if (brr[x] >= 0 && brr[y] >= 0 &&
brr[z] >= 0)
{
ans += String.fromCharCode(ch);
ans += ' ' ;
}
brr[x]++;
brr[y]++;
brr[z]++;
}
return ans;
}
let arr=[5, 6, 6 ];
let n = 3;
document.write(findCharacters(arr, n));
</script>
|
Time Complexity: O(n)
Last Updated :
27 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...