Some topological transformation and binary search
stretch the square onto a line with periodical boundaries -- namely a circle.the rest is a binary problem seemingly familiar.Code
stretch the square onto a line with periodical boundaries -- namely a circle.

the rest is a binary problem seemingly familiar.

# Code
```python3 []
class Solution:
    def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:
        dd = []
        for a,b in points:
            if a == 0:
                dd.append(b)
            elif b == side:
                dd.append(a+side)
            elif a == side:
                dd.append(side-b+2*side)
            else:
                dd.append(side-a+3*side)
        dd.sort()
        #print(dd)
        N = len(dd)
        def possible(d):
            I = bisect.bisect_left(dd,dd[0]+d)
            if N - I < k - 1: return False
            possible = False
            for i in range(I):
                curr = dd[i]
                count = 1
                j = i
                while j < N:
                    j = bisect.bisect_left(dd,curr+d)
                    if j >= N:
                        break
                    nxt = dd[j]
                    D = min(4*side-nxt+curr,nxt-dd[i],4*side+dd[i]-nxt)
                    if D < d: break
                    count += 1
                    curr = nxt
                    if count == k: return True
            return False
        l = 1
        r = dd[-1]-dd[0]
        while l < r:
            mid = (l+r) // 2
            if possible(mid):
                l = mid + 1
            else:
                r = mid
        return l-1



```
['Binary Search', 'Python3']