https://online.codingblocks.com/app/player/43575/content/20555/5059
Cant understand the logic of code in editorial
We can say that the minimum distance between the cows will lie between the range (max element - min element). The minimum distance between 2 stalls can be 0 (lower bound) and maximum distance is the range (upper bound). So, one can check for each value from lower bound to upper bound.
Let’s say for k minimum distance, we can check if it is possible to place cows in the stall. In case, you have reached the last stall but didn’t have placed all cows, then it is not possible else it is possible.
A point to note is that if, for a k, it is possible to place the cows. Then all the values less than k will be true. Also, if k is false, then all the values greater than k will be false. We can say it is creating a monotonic function and we have to check for the transition from true to false and return that value.
It can be easily done with Binary Search on the range for given array.
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.