Ankit_132

D

Jun 6th, 2023
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8. #define pb     push_back
  9. #define ppb    pop_back
  10.  
  11.  
  12. int main()
  13. {
  14.     _test
  15.     {
  16.         int n;
  17.         cin>>n;
  18.  
  19.         vector<int> a(n);
  20.         for(auto &e: a)
  21.             cin>>e;
  22.  
  23.         sort(a.begin(), a.end());
  24.  
  25.         auto possible = [&](int x)
  26.         {
  27.             int i=0, j=0;
  28.  
  29.             while(j<n && (a[j]-a[i]+1)/2<=x)     j++;
  30.             i = j;
  31.  
  32.             while(j<n && (a[j]-a[i]+1)/2<=x)     j++;
  33.             i = j;
  34.  
  35.             while(j<n && (a[j]-a[i]+1)/2<=x)     j++;
  36.             i = j;
  37.  
  38.             return (j == n);
  39.         };
  40.  
  41.         int ans = 1e9;
  42.         int low=0, high=1e9;
  43.  
  44.         while(low <= high)
  45.         {
  46.             int mid = (low+high)/2;
  47.  
  48.             if(possible(mid))
  49.                 ans=mid, high=mid-1;
  50.             else
  51.                 low=mid+1;
  52.         }
  53.  
  54.  
  55.         cout<<ans<<"\n";
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment