Wrong answer in few cases

hello sir i tried to go through the approach of binary search in this problem but i guess at the last in if else cases of the code its having error since its giving wrong answer for the ninth case of spoj could you tel what is the mistake which I am making
Below I have attached my code
#include
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
ll n,m;
cin>>n>>m;
ll mid;
ll sum=0;
ll arr[n];
// taking the input for the array
for(ll i=0;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
for(ll i=0;i<n;i++)
{
// cout<<arr[i]<<endl;
}
ll left=0;
ll right=arr[n-1];
while(left<=right)
{
mid=(left+right)/2;
// cout<<left<<" “<<right<<” “<<mid<<” "<<endl;
for(int i=0;i<n;i++)
{
if((arr[i]-mid)>0)
{
sum=sum+(arr[i]-mid);
}
}

//   cout<<"sum is given by"<<" "<<sum<<endl;
   // applying the proper binary search steps we would be getting
   if(sum>m)  left=mid+1;
   else if (sum==m) break;
   else if(sum<m)  right=mid-1;
   sum=0;

}
cout<<mid<<endl;

}
thanks :slight_smile:

Hi Kunal,
refer my code https://ide.codingblocks.com/s/637984

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.