PAINTERS PARTITION PROBLEM

#include
#include <bits/stdc++.h>
using namespace std;

bool isvalid(long int board[], long int k, long int n, long int time){
long int painters = 1;
long int ans =0;
for(int i =0;i<n;i++){
if(board[i] + ans > time){
ans =board[i];
painters++;
if(painters > k){
return false;
}
}
else{
ans+= board[i];
}
}
return true;
}

long int minTime(long int board[], long int n, long int k, long int e){
long int s = board[n-1];
long int mid = 0, ans = INT_MAX;

while(s <= e){
	mid = (s+e)/2;

	if(isvalid(board, k, n, mid)){
		e = mid -1 ;
		ans = min(ans, mid);
	}
	else{
		s = mid +1 ;
	}
}
return ans;

}

int main() {
long int k, n;
cin>>k>>n;
long int board[n], ms =0;
for(int i =0;i<n;i++){
cin>>board[i];
ms +=board[i];

}
sort(board, board+n);
cout<<minTime(board, n, k, ms)<<endl;
return 0;

}

This solution is showing some wrong testcases, Can you point out the mistake in my code ?

hello @shahpankti931 i have corrected your code .Now it is passing all the test cases.
https://ide.codingblocks.com/s/324024.
i hope i have cleared your doubt .
Happy Learning!!

I see you havent sorted the board array before applying binary search… WHY ??

hello @shahpankti931 you have sorted the board just to find the max value from the array and i have also done the same without doing the sorting.
i have done the same by just finding the max element while we arre taking the sum.

@shahpankti931 try to visualise the question we are applying binary search on our range but not on data .
for suppose take the sample test case.
our range is from 10 to 11
mid is 10
in this we are trying if we can assign the painters in time 10 so that they can do their work if it is possible we will try if they can do in time 9 otherwise we will try to do it in time 11.
i hope i have cleared your doubt .
Happy Learning !!

1 Like

Yes ! Got it, thanks

we dont have to minimise the number of painters its just that therer are k number iof painters avialable so the number of painters should not increase that .
we have to minimise the time taken by them .

hello @shahpankti931 as you are not responding to the doubt which you have raised a month before so you can now mark this doubt as resolved .
on off the chance if you still have doubt you can reopen it and ask again .
Happy Learning !!

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.