Advertisement
urmisaha

Adjacent array elements not divisible by 3

Nov 16th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. /** An array of integers is provided.
  2. You need to arrange the numbers such that no 2 consecutive numbers sum is divisible by 3.
  3. If no such arrangement is possible then return(“impossible”), else return the arrangement.
  4. **/
  5.  
  6. #include<bits/stdc++.h>
  7. #define pb push_back
  8.  
  9. using namespace std;
  10.  
  11. int T, N, x, z, o, t;
  12. vector<int> zeros;
  13. vector<int> ones;
  14. vector<int> twos;
  15. list<int> ans;
  16.  
  17. // Input:
  18. // 7
  19. // 11
  20. // 1 1 0 0 0 0 0 0 2 2 2
  21. // 9
  22. // 1 1 0 0 0 0 0 0 2
  23. // 6
  24. // 0 1 0 1 2 2
  25. // 4
  26. // 0 0 0 1
  27. // 4
  28. // 1 2 1 2
  29. // 4
  30. // 2 2 2 1
  31. // 5
  32. // 0 0 0 2 2
  33.  
  34. // Output:
  35. // 0 1 0 1 0 2 0 2 0 2 0
  36. // Impossible
  37. // 0 1 1 0 2 2
  38. // Impossible
  39. // Impossible
  40. // Impossible
  41. // 0 2 0 2 0
  42.  
  43.  
  44. int solve(){
  45.     z = zeros.size();
  46.     o = ones.size();
  47.     t = twos.size();    
  48.     if(!z && o && t)    // if no 0, but both 1 and 2
  49.         return 0;
  50.     if(z && !o && !t)   // if only 0
  51.         return 0;
  52.     if(o+t+1 < z)       // if more 0s than total+1 :: there can be maximum total+1 places for 0s for valid arrangement
  53.         return 0;
  54.     return 1;
  55. }
  56.  
  57. void arrange(){
  58.     if(o && t)          // if both 1s and 2s are there, keep aside a 0 to be kept in between the 1's series and 2's series
  59.         z--;
  60.     for(int i=0; i<o; ++i){     // fill up 0s and 1s
  61.         if(z)
  62.             ans.pb(zeros[--z]);
  63.         ans.pb(ones[i]);
  64.     }
  65.     if(o && t)          // if both 1s and 2s are there, the kept aside 0 needs to be put now
  66.         z++;
  67.     for(int i=0; i<t; ++i){     // fill up 0s and 2s
  68.         if(z)
  69.             ans.pb(zeros[--z]);
  70.         ans.pb(twos[i]);
  71.     }
  72.     if(z)               // if there's a last zero left
  73.         ans.pb(zeros[--z]);
  74.     for(auto it=ans.begin(); it!=ans.end(); ++it)
  75.         cout<<*it<<" ";
  76.     cout<<endl;
  77. }
  78.  
  79. int main(){
  80.     cin>>T;
  81.     while(T--){
  82.         cin>>N;
  83.         zeros.clear();
  84.         ones.clear();
  85.         twos.clear();
  86.         ans.clear();
  87.         for(int i=0; i<N; ++i){
  88.             cin>>x;
  89.             if(!(x%3))
  90.                 zeros.pb(x);
  91.             else if(x%3 == 1)
  92.                 ones.pb(x);
  93.             else
  94.                 twos.pb(x);
  95.         }
  96.         int res = solve();
  97.         if(!res)
  98.             cout<<"Impossible"<<endl;
  99.         else
  100.             arrange();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement