In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

Given the integer array position and the integer m. Return the required force.

&nbsp;
Example 1:

Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.


Example 2:

Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.


&nbsp;
Constraints:


	n == position.length
	2 &lt;= n &lt;= 105
	1 &lt;= position[i] &lt;= 109
	All integers in position are distinct.
	2 &lt;= m &lt;= position.length


class Solution:
  def possible (self,distance,positions,M):
    ball = 1 
    lastPos = positions[0]  
    for pos in positions:
      if pos-lastPos >= distance:
        ball+=1 
        if ball == M: return True 
        lastPos=pos 
     
    return False 
        
      
    
  def maxDistance(self, positions,M):
    positions.sort()
    low = 0 
    high = positions [-1] 
    ans = 0
    while low<=high:
      distance = low+(high-low)//2 
      
      if self.possible(distance,positions,M):
        ans = distance
        low=distance+1 
      else:
        high=distance-1 
    return ans 
class Solution {
    public int maxDistance(int[] position, int m) {
        Arrays.sort(position);
        int low=Integer.MAX_VALUE;
        int high=0;
        for(int i=1;i<position.length;i++){
            low=Math.min(low,position[i]-position[i-1]);
        }
        high=position[position.length-1]-position[0];
        int ans=-1;
        while(low<=high){
            int mid=low+(high-low)/2;
            if(blackbox(mid,position,m)){
                ans=mid;
                low=mid+1;
            }
            else{
                high=mid-1;
            }
        }
        return ans;
    }
		public boolean blackbox(int maxPossibleDist,int[] position, int m){
        int balls=1;
        int prevballplace=position[0];
        for(int i=1;i<position.length;i++){
            if(position[i]-prevballplace>=maxPossibleDist){
                prevballplace=position[i];
                balls++;
            }
        }
        if(balls>=m){
            return true;
        }
        return false;
    }
}
class Solution {
public:
    bool isPossible(vector<int>& position, int m,int mid){
        long long int basketCount=1;
        int lastPos=position[0];
        for(int i=0;i<position.size();i++){
            if((position[i]-lastPos)>=mid){
                basketCount++;
                lastPos=position[i];
            }
        }
        return basketCount>=m;
    }
    int maxDistance(vector<int>& position, int m) {
        sort(position.begin(),position.end());
        int n=position.size();
        long long int start=1;
        long long int end=position[n-1]-position[0];
        int ans=0;
        int mid= start+(end-start)/2;
        while(start<=end){
            if(isPossible(position,m,mid)){
            ans=mid;
            start=mid+1;
        }
        else
            end=mid-1;
           mid= start+(end-start)/2; 
        }
        return ans;
    }
};
var maxDistance = function(position, m) {
    position = position.sort((a, b) => a - b);

    function canDistribute(n) {
        let count = 1;
        let dist = 0;
        
        for(let i = 1; i < position.length; i++) {
            dist += position[i] - position[i - 1];
            
            if (dist >= n) {
                count++;
                dist = 0;
            }
        }
        
        return count >= m;
    }
    
    let left = 1;
    let right = position[position.length - 1] - position[0];
    let ans;
    
    while (left <= right) {
        const mid = Math.floor((left + right) / 2);

        if (canDistribute(mid)) {
            ans = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return ans;
};
Magnetic Force Between Two Balls