fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> rearrangeArray(vector<int>& a) {
  5. int n = a.size();
  6. vector<int>ans(2*n+1,1e9);
  7. int pos=0,neg=1;
  8. for(int i=0;i<n;i++){
  9. if(a[i]>0){
  10. ans[pos] = a[i];
  11. a[i]=1e9;
  12. pos=pos+2;
  13. }
  14. else{
  15. ans[neg] = a[i];
  16. a[i]=-1e9;
  17. neg = neg+2;
  18. }
  19. }
  20. for(int i=0;i<ans.size();i++)
  21. cout<<ans[i]<<" ";
  22. return ans;
  23. }
  24. int main() {
  25. vector<int>a = {3,2,5,4,-11,-12};
  26. vector<int>v = rearrangeArray(a);
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
3 -11 2 -12 5 1000000000 4 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000