Easy 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)$$ -->

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

# Code
```cpp []
class Solution {
public:
    int maxDistance(int S, vector<vector<int>>& P, int K) {
        vector<long long> v1(100, 0);
        unordered_map<long long, long long> m1;
        set<long long> s1;
        priority_queue<long long> pq1;
        stack<long long> st1;
        queue<long long> q1;

        long long n = P.size(), l = 4LL * S;
        vector<uint64_t> a(n);
        
        size_t i = 0;
        while (i < n) {
            int x = P[i][0], y = P[i][1];
            uint64_t p;
            if (y == 0) p = x;
            else if (x == S) p = S + y;
            else if (y == S) p = 2ULL * S + (S - x);
            else p = 3ULL * S + (S - y);
            a[i] = p;
            ++i;
        }

        sort(a.begin(), a.end());
        vector<uint64_t> b(2 * n);
        
        i = 0;
        while (i < n) {
            b[i] = a[i];
            b[i + n] = a[i] + l;
            ++i;
        }

        return X::y(a, b, K, l);
    }

private:
    class X {
    public:
        static bool z(uint64_t d, const vector<uint64_t>& a, const vector<uint64_t>& b, int K, uint64_t l) {
            uint64_t n = a.size(), m = 2 * n;
            vector<int> c(m, m);
            int i = 0;
            
            while (i < m) {
                uint64_t t = b[i] + d;
                int j = (int)(lower_bound(b.begin(), b.end(), t) - b.begin());
                c[i] = j;
                ++i;
            }

            int L = 32;
            vector<vector<int>> jmp(L, vector<int>(m, m));
            
            for (i = 0; i < m; i++) {
                jmp[0][i] = c[i];
            }
            for (int j = 1; j < L; j++) {
                for (i = 0; i < m; i++) {
                    int ni = jmp[j - 1][i];
                    jmp[j][i] = (ni < m ? jmp[j - 1][ni] : m);
                }
            }

            for (i = 0; i < n; i++) {
                int cur = i, r = K - 1;
                for (int j = 0; j < L; j++) {
                    if (r & (1 << j)) {
                        cur = jmp[j][cur];
                        if (cur >= i + n) break;
                    }
                }
                if (cur < i + n && (b[i] + l - b[cur] >= d)) {
                    return true;
                }
            }
            return false;
        }

        static int y(const vector<uint64_t>& a, const vector<uint64_t>& b, int K, uint64_t l) {
            uint64_t lo = 0, hi = 2ULL * l, ans = 0;
            while (lo <= hi) {
                uint64_t mid = (lo + hi) / 2;
                if (z(mid, a, b, K, l)) {
                    ans = mid;
                    lo = mid + 1;
                } else {
                    hi = mid - 1;
                }
            }
            return ans;
        }
    };
};
```
['Math', 'Ordered Map', 'C++']