Solution | cpp | binary search | nice solution


#1
string Solution::solve(vector<int> &A, string B) {
     long long n = A.size();
     auto isOk = [&](long long median, long long b, long long operation) {
         for(int i=n/2; i < n; ++i) {
             if(median - A[i] > 0)
                operation += (median - A[i]);
            if(operation > b)
                return false;
         }
         return (operation <= b);
     };
     
     long long b = stoll(B);
     long long lo = 0;
     long long hi = A.back() + b+1;
     while(lo<hi) {
         long long mid = (lo+hi+1)/2;
         if(isOk(mid, b, 0LL))
            lo = mid;
         else
            hi = mid-1;
        
     }
     return to_string(lo);
}