Codechef Starters 57 Division 3 (Rated) Problem Code: MAXEXP Maximum Expression Solution
Problem Link: https://www.codechef.com/START57C/problems/MAXEXP
Solution in C++:
/// La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
/// Abul Hasnat Tonmoy
/// Department of CSE,23rd batch
/// Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t, n, i;
cin >> t;
while (t--) {
string s;
cin >> n >> s;
string temp = "";
string symbol = "";
for (int i = 0; i < n; i++) {
if (s[i] != '+' && s[i] != '-')
temp += s[i];
else
symbol += s[i];
}
sort(temp.begin(), temp.end());
sort(symbol.begin(), symbol.end());
int ns = temp.size();
int ss = symbol.size();
int k = ss - 1;
string ans = "";
for (i = 0; i < ns; i++) {
if (k >= 0) {
ans += temp[i];
ans += symbol[k];
k--;
} else
ans += temp[i];
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
}
return 0;
}
No comments