Huhu
unknown
c_cpp
2 years ago
1.2 kB
18
Indexable
#include <bits/stdc++.h>
#define ll long long
#define LIM 105
#define X first
#define Y second
#define EL cout<<"\n"
using namespace std;
vector<int> rearrangeArray(vector<int>& nums) {
vector<int> positive;
vector<int> negative;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > 0) {
positive.push_back(nums[i]);
}
else {
negative.push_back(nums[i]);
}
}
int pt1 = 0, pt2 = 0;
vector<int> res; int flag = 0;
while (pt1 < positive.size() && pt2 < negative.size()) {
if (flag = 0) {
res.push_back(positive[pt1]);
pt1++;
flag = 1;
}
else if (flag = 1) {
res.push_back(negative[pt2]);
pt2++;
flag = 0;
}
}
return res;
}
int main() {
vector<int> vt;
int n; cin >> n;
for (int i = 0; i < n; i++) {
int x; cin >> x;
vt.push_back(x);
}
vt = rearrangeArray(vt);
for (int i = 0; i < n; i++) {
cout << vt[i] << " ";
}
cout << endl;
return 0;
}Editor is loading...
Leave a Comment