EKO - Eko - SPOJ Solution C++

  Problem Link : EKO 


👉 Hint : Advanced binary search

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll arr[1000001];
ll m;
bool f(ll mid,ll n)
{
    ll i;
    ll diff=0;
    for(i=0;i<n;i++)
    {
        if(arr[i]>mid)
        {
            diff+=arr[i]-mid;
        }
        if(diff>=m)
            return true;
    }
    return false;
}
int main()
{
    ll n;
    cin>>n>>m;
    ll i,max=INT_MIN;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i]>max)
            max=arr[i];
    }
    ll low=0;
    ll high=max;
    while(low<high)
    {
        ll mid=low+(high-low+1)/2;
        if(f(mid,n))
            low=mid;
        else
            high=mid-1;
    }
    cout<<high;
    return 0;
 
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!


1 comments:

Click here for comments
CODER D
admin
May 7, 2022 at 10:38 PM ×

Giving Wrong Answer , Not passing TC 9 .

Reply
avatar