I tried writting code. please check?
Painter's board question
hello @mittali
a)ur possible function is bit incorrect.
u need to keep track of painter count and then timing of current painter seprately
b) the array is not sorted so find max explicitly and then assign it to s
why do we need to keep a track of minutes. we just need to return number of painters from possible function.
#include #include using namespace std; int possible(int s[], int k, int n, int mid){ int painter = 1; for(int i=0; i<n; i++){ int sum = 0; if(painters[i]<=mid){ painter++; sum += painters[i]; } else{ return painter-1; } } return painter-1; } int minutestaken(int s[], int k, int n){ int sum=0; for(int i=0; i<n; i++){ sum+=s[i]; } int s = s[n-1]; int e = sum; int ans = INT_MAX; while(s<=e){ int mid = (s+e)/2; if(possible(a,k,n,mid)<=k){ ans = min(ans,mid); e = mid-1; } else{ s = mid+1; } } return ans; } int main() { int k,n; cin>>k>>n; int a[n]; for(int i=0; i<n; i++){ cin>>a[i]; } int s = sort(a,a+n); cout<<minutestaken(s, k, n)<<endl; }
I am not getting this question. how to solve
…
becuase in possible function each painter can work for atmax mid number of minutes.
…
once rewatch the binary search playlist ,it is covered there
its wrong.
a) u cant sort the array , as painter can paint only contigous block ,so order matters
b) ur possible function is wrong.
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.