#include <bits/stdc++.h>
using namespace std;
// Function to generate the maximum possible number
string getLargestNumber(string num) {
int N = num.length();
string ans = "";
vector<int> freq(10);
int left = 0;
// Two-pointer technique to find windows of same parity consecutive elements
while (left < N) {
int right = left;
// Find a window of consecutive elements with the same parity
while (right < (N - 1) && (num[right] - '0') % 2 == (num[right + 1] - '0') % 2) {
++freq[num[right] - '0'];
right++;
}
// Add the last element of the window
++freq[num[right] - '0'];
// Add elements in descending order for the current window
for (int i = 9; i >= 0; i--) {
while (freq[i] > 0) {
ans += to_string(i);
--freq[i];
}
}
// Move to the next window
left = right + 1;
}
return ans;
}
// Driver function to test getLargestNumber
int main() {
// Test input
string num = "0082663";
cout << getLargestNumber(num) << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byBnZW5lcmF0ZSB0aGUgbWF4aW11bSBwb3NzaWJsZSBudW1iZXIKc3RyaW5nIGdldExhcmdlc3ROdW1iZXIoc3RyaW5nIG51bSkgewogICAgaW50IE4gPSBudW0ubGVuZ3RoKCk7CiAgICBzdHJpbmcgYW5zID0gIiI7CiAgICB2ZWN0b3I8aW50PiBmcmVxKDEwKTsKICAgIGludCBsZWZ0ID0gMDsKCiAgICAvLyBUd28tcG9pbnRlciB0ZWNobmlxdWUgdG8gZmluZCB3aW5kb3dzIG9mIHNhbWUgcGFyaXR5IGNvbnNlY3V0aXZlIGVsZW1lbnRzCiAgICB3aGlsZSAobGVmdCA8IE4pIHsKICAgICAgICBpbnQgcmlnaHQgPSBsZWZ0OwoKICAgICAgICAvLyBGaW5kIGEgd2luZG93IG9mIGNvbnNlY3V0aXZlIGVsZW1lbnRzIHdpdGggdGhlIHNhbWUgcGFyaXR5CiAgICAgICAgd2hpbGUgKHJpZ2h0IDwgKE4gLSAxKSAmJiAobnVtW3JpZ2h0XSAtICcwJykgJSAyID09IChudW1bcmlnaHQgKyAxXSAtICcwJykgJS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