<p>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 <code>n</code> empty baskets, the <code>i<sup>th</sup></code> basket is at <code>position[i]</code>, Morty has <code>m</code> balls and needs to distribute the balls into the baskets such that the <strong>minimum magnetic force</strong> between any two balls is <strong>maximum</strong>.</p>

<p>Rick stated that magnetic force between two different balls at positions <code>x</code> and <code>y</code> is <code>|x - y|</code>.</p>

<p>Given the integer array <code>position</code> and the integer <code>m</code>. Return <em>the required force</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg" style="width: 562px; height: 195px;" />
<pre>
<strong>Input:</strong> position = [1,2,3,4,7], m = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == position.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= position[i] &lt;= 10<sup>9</sup></code></li>
	<li>All integers in <code>position</code> are <strong>distinct</strong>.</li>
	<li><code>2 &lt;= m &lt;= position.length</code></li>
</ul>

class Solution:
    def maxDistance(self, position: List[int], m: int) -> int:


        '''
        pos[i]

        m -min mag. force is maximum? 

        |x-y|

        [1, 2, 3, 4, 7], [m = 3]
        ^

        [1 2 3 4 5 6 7]
         ^ ^ ^ ^     ^

        7/3 = 2 between balls


        [1 2 3 4 5 ...... 1000000000]
         ^       ^           ^

        closes to the mid = 500000000? 
        5

        1 and 

             
                              false
        1 2 3 3 4 5  6 7 8.   500000000    .....10000000
        ^                      ^                  ^

        
        greedy or not?  3 balls vs 4 balls 


   

        [1 2 3 4 5 ...... 1000000000]
         ^       ^           ^

        binary search on answers? 



        diff = 4 ; 1,4,1000000000
        force: 4, 99999999999999. #minimum magnetic force between any two balls is maximum.

        diff = 1000000000 -1

    
        difference between 2 pairs maximized, m/2 pairs


        [1,2,3,4,5,6,7,8,9,....15,17], m = 3
         ^                      ^  

        1 and mid =9: 1,15, 15+9 > max - can't ; false
        1 and distance = lo=1, hi = 9-1 ==8;
          
        checking 8:
        1+ 8

        8+8  
        true 


        diff: 8
        

        result = 2

        //diff = 9?
        def canBeAnswer(mid): 
            
            positions[9] = 15
            diff1 = 2
            diff2 = 9

            return false 

        lo = 1
        hi = 17

        while lo < hi: 
            mid = (1+17)//2

            if canBeAnswer(mid):


          


        
        '''

        position.sort()
        low, high = 1, position[-1] - position[0]


        
        def canBeAnswer(min_force):

            count = 1
            last_position = position[0]
            
        
            for i in range(1, len(position)):
                if position[i] - last_position >= min_force:
                    count += 1
                    last_position = position[i]
                    if count == m:
                        return True
            
    
        result = 0
        while low <= high:
            mid = (low + high) // 2
            if canBeAnswer(mid):
                result = mid  # 
                low = mid + 1
            else:
                high = mid - 1
        
        return result



        
30200