Ankit_132

B

Oct 22nd, 2023
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7.  
  8. int main()
  9. {
  10.     _test
  11.     {
  12.         int n, k;
  13.         cin>>n>>k;
  14.  
  15.         vector<int> a(n);
  16.         for(auto &e: a)     cin>>e;
  17.  
  18.         if(k==2 || k==3 || k==5)
  19.         {
  20.             int ans = 10;
  21.             for(int i=0; i<n; i++)
  22.                 ans = min(ans, ((a[i]+k-1)/k)*k - a[i]);
  23.  
  24.             cout<<ans<<"\n";
  25.             continue;
  26.         }
  27.  
  28.         int ans = 10;
  29.         int cnt = 0;
  30.         for(int i=0; i<n; i++)
  31.         {
  32.             ans = min(ans, ((a[i]+k-1)/k)*k - a[i]);
  33.             cnt += (a[i]%2 == 0);
  34.         }
  35.  
  36.         cnt = min(cnt, 2);
  37.  
  38.         ans = min(ans, 2-cnt);
  39.  
  40.         cout<<ans<<"\n";
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment