# Minimum addition/removal of characters to be done to make frequency of each character prime

Given a string **S** of length **N**, the task is to find the minimum operations required to make the frequency of every distinct character prime. Frequency of a character can be increased or decreased by 1 in a single operation.

**Examples:**

Input:S = “abba”

Output:0

Explanation:There are two characters in the string S and the frequency of ‘a’ is 2 which is prime. The frequency of ‘b’ is 2 which is also a prime number.Hence no operations are required for this case.

Input:S = “aaaaaaaaa”

Output:2

Explanation:The frequency of ‘a’ in the string is 9. Either remove 2 a’s from the string or add 2 a’s in the string to make the frequency of ‘a’ prime. Thus minimum number of operations needed is 2.

**Naive Approach:**

Find the frequency of each character in the string. Let the frequency be **X**. Find the prime number greater than X and less than X. Compare the difference between **X** and the two prime numbers found and choose the closest prime number. Add the difference between the closest prime number and **X** to the number of operations. Thus the minimum number of operations will be obtained. It is an inefficient approach as the lower and upper bound to find the prime number is not known.

**Efficient Approach :**

- Use the sieve algorithm to find all the prime numbers up to N and store them in an array.
- Find out the nearest lower prime number for all the numbers from
**i = 1 to N**and store the difference between**i**and its nearest lower prime number in an array say**arr1[]**. - Find out the nearest higher prime number for all the numbers from
**i = 1 to N**and store the difference between**i**and its nearest higher prime number in an array say**arr2[]**. - Traverse the string and find the frequency of all the distinct characters in the string and use an unordered_map to save the frequencies of these distinct characters.
- Let the frequency of any distinct character be X then find out the distance between X and nearest prime number from the arrays
**arr1[]**and**arr2[]**. - Choose the distance which is lesser between the two and add this distance to the number of operations.
- Do this for all the distinct characters and print the number of operations finally.

Below is the implementation of the above approach:

`// C++ code for the above approach ` ` ` `#include <iostream> ` `#include <unordered_map> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `// using the seive to find ` `// the prime factors. ` `void` `spf_array(` `int` `spf[], ` `int` `d) ` `{ ` ` ` `spf[1] = 1; ` ` ` `int` `i = 0; ` ` ` ` ` `// setting every number ` ` ` `// as prime number initailly ` ` ` `for` `(i = 2; i <= d; i++) { ` ` ` `spf[i] = i; ` ` ` `} ` ` ` ` ` `// removing the even numbers as ` ` ` `// they cannot be prime except 2 ` ` ` `for` `(i = 2; i <= d; i = i + 2) { ` ` ` `spf[i] = 2; ` ` ` `} ` ` ` ` ` `for` `(i = 3; i * i <= d; i++) { ` ` ` `// if they are prime ` ` ` `if` `(spf[i] == i) { ` ` ` `int` `j; ` ` ` ` ` `// iterating the loop for ` ` ` `// prime and eliminate all ` ` ` `// the multiples of three ` ` ` `for` `(j = i * i; j <= d; j = j + i) { ` ` ` `if` `(spf[j] == j) { ` ` ` `spf[j] = i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// function to find the closest ` `// prime number of every ` `// number upto N (size of the string) ` `void` `closest_prime_spf(` `int` `spf[], ` ` ` `int` `cspf[], ` `int` `d) ` `{ ` ` ` `// for the base case ` ` ` `cspf[1] = 1; ` ` ` ` ` `int` `i = 0, j = 0, k = 0; ` ` ` ` ` `// iterating to find the ` ` ` `// distance from the ` ` ` `// lesser nearest prime ` ` ` `for` `(i = 2; i < d; i++) { ` ` ` `if` `(spf[i] != i) { ` ` ` `cspf[i] = ` `abs` `(i - k); ` ` ` `} ` ` ` `else` `{ ` ` ` `cspf[i] = -1; ` ` ` `k = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// iterating to find the ` ` ` `// distance from the ` ` ` `// lesser nearest prime ` ` ` `for` `(i = d - 1; i >= 2; i--) { ` ` ` `if` `(spf[i] != i) { ` ` ` `if` `(cspf[i] > ` `abs` `(k - i)) { ` ` ` `cspf[i] = ` `abs` `(k - i); ` ` ` `} ` ` ` `} ` ` ` `else` `{ ` ` ` `k = i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// function to find the ` `// minimum operation ` `int` `minimum_operation(` `int` `cspf[], ` ` ` `string s) ` `{ ` ` ` ` ` `// created map to find the ` ` ` `// frequency of distinct characters ` ` ` `unordered_map<` `char` `, ` `int` `> m; ` ` ` ` ` `// variable to iterate and ` ` ` `// holding the minimum operation ` ` ` `int` `i = 0, k = 0; ` ` ` ` ` `// loop for calculation frequency ` ` ` `for` `(i = 0; i < s.length(); i++) { ` ` ` `m[s[i]] = m[s[i]] + 1; ` ` ` `} ` ` ` ` ` `// iterate over frequency ` ` ` `// if we not get a chcarter ` ` ` `// frequency prime ` ` ` `// then we find the closest ` ` ` `// prime and add ` ` ` `for` `(` `auto` `x : m) { ` ` ` `int` `h = x.second; ` ` ` `if` `(cspf[h] != -1) { ` ` ` `k = k + cspf[h]; ` ` ` `} ` ` ` `} ` ` ` `return` `k; ` `} ` ` ` `// Function to find the ` `// minimum number of operations ` `void` `minOper(string s) ` `{ ` ` ` ` ` `int` `spf[s.length() + 1]; ` ` ` `int` `cspf[s.length() + 1]; ` ` ` ` ` `// function called to create ` ` ` `// the spf ` ` ` `spf_array(spf, s.length() + 1); ` ` ` ` ` `// function called to ` ` ` `// create the cspf ` ` ` `closest_prime_spf(spf, cspf, ` ` ` `s.length() + 1); ` ` ` ` ` `cout << minimum_operation(cspf, s) ` ` ` `<< endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// input string ` ` ` `string s = ` `"aaaaaaaaabbcccccc"` `; ` ` ` ` ` `minOper(s); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time complexity:** O(N * log(log(N)))

**Auxillary Space complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Minimum characters to be replaced to make frequency of all characters same
- Minimum operations to make frequency of all characters equal K
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Minimum deletions required to make frequency of each letter unique
- Minimum operation require to make first and last character same
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum sum of squares of character counts in a given string after removing k characters
- Minimum characters required to make a password strong
- Minimum characters to be deleted from the end to make given two strings equal
- Minimum insertions to make a Co-prime array
- Minimum characters to be added at front to make string palindrome
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum changes required to make all Array elements Prime
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum number of characters to be removed to make a binary string alternate
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Find the minimum number to be added to N to make it a prime number

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.