Advertisement
fahimkamal63

array if alternate +ve and -ve

May 17th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. using namespace std;
  5. int main(){
  6.     int t; scanf("%d", &t);
  7.     while(t--){
  8.         int n; scanf("%d", &n);
  9.         vector<int> positive;
  10.         vector<int> negative;
  11.         for(int i = 0; i < n; i++){
  12.             int k; scanf("%d", &k);
  13.             if(k < 0){
  14.                 negative.push_back(k);
  15.             }
  16.             else positive.push_back(k);
  17.         }
  18.         int count = 0;
  19.         auto a = positive.begin();
  20.         auto b = negative.begin();
  21.         while((a != positive.end()) && (b != negative.end())){
  22.             count++;
  23.             if(count % 2){
  24.                 printf("%d ", *a++);
  25.                 //positive.erase(positive.begin());
  26.             }
  27.             else {
  28.                 printf("%d ", *b++);
  29.                // negative.erase(negative.begin());
  30.             }
  31.         }
  32.         if(a == positive.end()){
  33.             while(b != negative.end()){
  34.                 printf("%d ", *b++);
  35.                 //negative.erase(negative.begin());
  36.             }
  37.         }
  38.         else{
  39.             while(a != positive.end()){
  40.                 printf("%d ", *a++);
  41.                 //positive.erase(positive.begin());
  42.             }
  43.         }
  44.         printf("\n");
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement