#include <bits/stdc++.h>
using namespace std;
void solve(vector<int> &nums) {
// Method 1: Sorting
// Time: O(n * log(n)), Space: O(1)
// sort(nums.begin(), nums.end());
// Method 2: Counting 0s, 1s and 2s
// Time: O(n), Space: O(n)
// int zeros = 0, ones = 0, twos = 0;
// for (int i = 0; i < nums.size(); i++) {
// switch (nums[i]) {
// case 0:
// zeros++;
// break;
// case 1:
// ones++;
// break;
// case 2:
// twos++;
// break;
// }
// }
// for (int i = 0; i < zeros; i++) {
// nums[i] = 0;
// }
// for (int i = zeros; i < zeros + ones; i++) {
// nums[i] = 1;
// }
// for (int i = zeros + ones; i < nums.size(); i++) {
// nums[i] = 2;
// }
// Method 3: Dutch National Flag Algorithm
// Time: O(n), Space: O(1)
int low = 0, mid = 0, high = nums.size() - 1;
while (mid <= high) {
switch (nums[mid]) {
case 0:
swap(nums[low], nums[mid]);
low++;
mid++;
break;
case 1:
mid++;
break;
case 2:
swap(nums[mid], nums[high]);
high--;
break;
}
}
}
void print(vector<int> &nums) {
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
cout << endl;
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
solve(nums);
print(nums);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIHNvbHZlKHZlY3RvcjxpbnQ+ICZudW1zKSB7CgkvLyBNZXRob2QgMTogU29ydGluZwoJLy8gVGltZTogTyhuICogbG9nKG4pKSwgU3BhY2U6IE8oMSkKCS8vIHNvcnQobnVtcy5iZWdpbigpLCBudW1zLmVuZCgpKTsKCQoJLy8gTWV0aG9kIDI6IENvdW50aW5nIDBzLCAxcyBhbmQgMnMKCS8vIFRpbWU6IE8obiksIFNwYWNlOiBPKG4pCgkvLyBpbnQgemVyb3MgPSAwLCBvbmVzID0gMCwgdHdvcyA9IDA7CgkvLyBmb3IgKGludCBpID0gMDsgaSA8IG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