Maximize palindromic strings of length 3 possible from given count of alphabets
Given an array arr[] of size 26, representing frequencies of character ‘a’ to ‘z’, the task is to find the maximum number of palindromic strings of length 3 that can be generated from the specified count of alphabets.
Examples:
Input: arr[] = {4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Output: 3
Explanation:One of the possible solution is to take two characters of ‘b’ and one character of ‘a’, making string “bab”. Now, 3 ‘a’ and 3 ‘b’ are left from which strings “aaa” and “bbb” can be made. Thus total of 3 palindromic strings can be formed.
Input: arr[] = {2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Output: 1
Explanation: The only possible palindromic string is “aba”.
Approach: The given problem can be solved based on the following observations:
- The number of strings can be maximized by first making possible strings only of one type of character.
- After that take two characters of one type and one character of the other kind and forming a string.
- If still characters which occur more than 1 exist, then take 3 among such characters and form 2 strings of type “ACA” and “BCB”.
- If again two characters are left which occurs 2 times, then form a string and add one more to answer.
- After this, only characters that occur only once or a character that occurs twice will be left.
Follow the steps below to solve the problem:
- Initialize a variable, res with 0 to store the count of maximum palindromic strings of size 3 that can be formed.
- Initialize a variable, C1, and C2 with 0 to store the count of characters having frequency one and two respectively
- Iterate over the array, arr[] and increment the res by arr[i]/3, set arr[i] to arr[i]%3, and increment C1 by 1 if arr[i]%3 is 1, otherwise, increment C2 by 1 if arr[i]%3 is 2.
- After traversal, increment res by min (C1, C2) and update C1 and C2. Then, increment res by twice of C2/3 and update C2. Lastly, increment res by C2/2.
- Finally, print res as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximum_pallindromic( int arr[])
{
int res = 0;
int c1 = 0, c2 = 0;
for ( int i = 0; i < 26; i++) {
res += arr[i] / 3;
arr[i] = arr[i] % 3;
if (arr[i] == 1)
c1++;
else if (arr[i] == 2)
c2++;
}
res += min(c1, c2);
int t = min(c1, c2);
c1 -= t;
c2 -= t;
res += 2 * (c2 / 3);
c2 %= 3;
res += c2 / 2;
cout << res;
}
int main()
{
int arr[] = { 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
maximum_pallindromic(arr);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void maximum_pallindromic( int arr[])
{
int res = 0 ;
int c1 = 0 , c2 = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
res += arr[i] / 3 ;
arr[i] = arr[i] % 3 ;
if (arr[i] == 1 )
c1++;
else if (arr[i] == 2 )
c2++;
}
res += Math.min(c1, c2);
int t = Math.min(c1, c2);
c1 -= t;
c2 -= t;
res += 2 * (c2 / 3 );
c2 %= 3 ;
res += c2 / 2 ;
System.out.print(res);
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
maximum_pallindromic(arr);
}
}
|
Python3
def maximum_pallindromic(arr):
res = 0
c1 = 0
c2 = 0
for i in range ( 26 ):
res + = arr[i] / / 3
arr[i] = arr[i] % 3
if (arr[i] = = 1 ):
c1 + = 1
elif (arr[i] = = 2 ):
c2 + = 1
res + = min (c1, c2)
t = min (c1, c2)
c1 - = t
c2 - = t
res + = 2 * (c2 / / 3 )
c2 % = 3
res + = c2 / / 2
print (res)
if __name__ = = "__main__" :
arr = [ 4 , 5 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
maximum_pallindromic(arr)
|
C#
using System;
public class GFG
{
static void maximum_pallindromic( int []arr)
{
int res = 0;
int c1 = 0, c2 = 0;
for ( int i = 0; i < 26; i++)
{
res += arr[i] / 3;
arr[i] = arr[i] % 3;
if (arr[i] == 1)
c1++;
else if (arr[i] == 2)
c2++;
}
res += Math.Min(c1, c2);
int t = Math.Min(c1, c2);
c1 -= t;
c2 -= t;
res += 2 * (c2 / 3);
c2 %= 3;
res += c2 / 2;
Console.Write(res);
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
maximum_pallindromic(arr);
}
}
|
Javascript
<script>
function maximum_pallindromic(arr)
{
var res = 0;
var c1 = 0, c2 = 0;
for ( var i = 0; i < 26; i++) {
res += parseInt(arr[i] / 3);
arr[i] = (arr[i] % 3);
if (arr[i] == 1)
c1++;
else if (arr[i] == 2)
c2++;
}
res += Math.min(c1, c2);
var t = Math.min(c1, c2);
c1 -= t;
c2 -= t;
res += 2 * parseInt(c2 / 3);
c2 %= 3;
res += parseInt(c2 / 2);
document.write( res);
}
var arr = [ 4, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 ];
maximum_pallindromic(arr);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
27 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...