-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-12-24 Sort 0s, 1s and 2s.cpp
57 lines (47 loc) · 1.13 KB
/
5-12-24 Sort 0s, 1s and 2s.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
void sort012(vector<int>& arr) {
// code here
int countZero = 0, countOne = 0, countTwo = 0;
for(int i = 0; i<arr.size(); i++){
if(arr[i] == 0 ) countZero++;
else if(arr[i] == 1) countOne++;
else countTwo++;
}
for(int i = 0; i< arr.size(); i++){
if(countZero >0 ) arr[i] = 0 , countZero--;
else if(countOne > 0) arr[i] = 1 , countOne--;
else arr[i] = 2;
}
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> a;
string input;
int num;
getline(cin, input);
stringstream s2(input);
while (s2 >> num) {
a.push_back(num);
}
Solution ob;
ob.sort012(a);
int n = a.size();
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends