// test case 2 4 10 20 30 40
#include<iostream>
#include<algorithm>
using namespace std;
bool isvalid(int k,int n,int a[],int mid)
{ int time=0;
int painter_used=1;
for (int i = 0; i < n; i++)
{ time=time+a[i];
if (time>=mid)
{ time=a[i];
painter_used++;
}
}
return painter_used<=k;
}
int main(){
int k;int n;
cin>>k>>n;
int a[n];
int sum=0;
for (int i = 0; i < n; i++)
{
cin>>a[i];
sum+=a[i];
}
int start=*max_element(a,a+n);
int end=sum;
int ans=0;
while (start<=end)
{
int mid=(start+end)/2;
if (isvalid(k,n,a,mid))
{ ans=mid;
end=mid-1;
}
else
{
start=mid+1;
}
}
cout<<ans;
return 0;
}
I am getting 1 extra in my output
Here’s your debugged code
why you updated the ans =start???
This is because we want maximum painter used and wherever our start will point, it will be our maximum painter.
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.