easy cpp solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(n *logn * log(side))

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(n)

# Code
```cpp []
class Solution {
    int find(vector<vector<int>>& p,vector<int>& po,int m,int l){
        int r= p.size()-1,ans= p.size();
        while(l<=r){
            int mid= l+(r-l)/2;
            if(abs(p[mid][0]-po[0])+abs(p[mid][1]-po[1])>= m){
                ans= mid; r=mid-1;
            }
            else l=mid+1;
        }
        return ans;
    }
bool solve(int mid,vector<vector<int>>& p,int k){
    for(int j=0;j<p.size();j++){
        int t=0; vector<int> prev,first; bool flag=0;
        int idx=j;
        while(idx< p.size()){
            t++; 
           int i= find(p,p[idx],mid,idx+1);
           if(!flag){flag=1; first= p[idx];}
           prev= p[idx];
           idx=i; 
           if(t>k) return 1;
        }
      if( t>1 &&  abs(first[0]-prev[0])+abs(first[1]-prev[1]) < mid) t--;
      if(t>=k)return 1;
    }
    return 0;
}
public:
    int maxDistance(int side, vector<vector<int>>& po, int k) {
        int l=0,r=side*2, ans=0; if(k>4) r=side;
        sort(po.begin(),po.end());
        vector<vector<int>> p,po1; vector<vector<int>> p0,p1,p2,p3;
        for(auto i:po){
            if(i[1]==0) p0.push_back(i);
           else  if(i[0]==side) p1.push_back(i);
            else if(i[1]==side) p2.push_back(i);
             else if(i[0]==0) p3.push_back(i);
        }
       if(p0.size()>0) for(auto i:p0) p.push_back(i);
        if(p1.size()>0)for(auto i:p1) p.push_back(i);
        if(p2.size()>0){reverse(p2.begin(),p2.end()); 
        for(auto i:p2) p.push_back(i);}
        if(p3.size()>0){
        reverse(p3.begin(),p3.end());
        for(auto i:p3) p.push_back(i);}
        while(l<=r){
            int mid= l+(r-l)/2;
            if(solve(mid,p,k)){ans=mid; l=mid+1;}
            else r=mid-1;
        }
        for(auto i:p) cout << i[0] <<","<< i[1]<<" ";
        cout << endl;
        for (auto i:po1) cout << i[0] <<","<< i[1]<<" ";
        return ans;
    }
};
```
['Binary Search', 'C++']