Mother's Day On Mother's Day Priyanshu is unable to tell his mother how special she is…
- User
Mother's Day
On Mother's Day Priyanshu is unable to tell his mother how special she is for him. So he decides to write a letter for her expressing gratitude towards her. He wrote a string S in the letter having length L. There are some of the grammatical mistakes in his string, which could be resolved if he sorts the string Sin such a way that when individual characters are converted to their ASCII values, they should
follow given rules:
Characters possessing prime ASCII value should come before characters possessing composite ASCII value.
If two characters are having prime ASCII value, character having less ASCII should come before other.
If two characters are having composite ASCII value, character having greater ASCII value should come before other
Input format
The first line of input contains the length N of string S
The second line of input contains string S.
Output format
The single line of output should contain the required modified String.
Constraints
1< Length(S)≤ 10
33< ASCII value of character < 126
Sample Input
13 Khokharnikunj
Sample Output
akkuroonjjhhk
Explanation
for primes: a <k
for Composite: K<h<i<j<n<o<r<u
Adding up logic, required answer is akkuronnjihhk. Please provide an efficient C++ code snippet using the std namespace, without any comments and small variable names .
- Assistant
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<cmath> using namespace std; bool isP(int n){ if(n<2)return 0; for(int i=2;i<=sqrt(n);i++)if(n%i==0)return 0; return 1; } int main(){ int n;cin>>n; string s;cin>>s; vector<char>p,c; for(char x:s){ int v=x; if(isP(v))p.push_back(x); else c.push_back(x); } sort(p.begin(),p.end()); sort(c.begin(),c.end(),greater<char>()); for(char x:p)cout<<x; for(char x:c)cout<<x; }