💯🔥 100% Beats Across 11 Languages 🚀🎉
Code
# Code
```java []
import java.util.*;

public class Solution {
    public int maxDistance(int side, int[][] points, int k) {
        int n = points.length;
        long[] pos = new long[n];
        for (int i = 0; i < n; i++) {
            int x = points[i][0], y = points[i][1];
            long p;
            if (y == 0)
                p = x;
            else if (x == side)
                p = (long) side + y;
            else if (y == side)
                p = 2L * side + (side - x);
            else // x == 0
                p = 3L * side + (side - y);
            pos[i] = p;
        }
        Arrays.sort(pos);
        long L = 4L * side;
        int total = n * 2;
        long[] ext = new long[total];
        for (int i = 0; i < n; i++){
            ext[i] = pos[i];
            ext[i+n] = pos[i] + L;
        }
        
        long low = 0, high = 2L * side;
        while (low < high) {
            long mid = (low + high + 1) / 2;
            if (canPlace(mid, k, n, ext, L))
                low = mid;
            else
                high = mid - 1;
        }
        return (int) low;
    }
    
    private boolean canPlace(long d, int k, int n, long[] ext, long L) {
        for (int start = 0; start < n; start++) {
            int cur = start;
            long last = ext[start];
            boolean valid = true;
            int limit = start + n;
            for (int step = 1; step < k; step++){
                long target = last + d;
                int lo = cur + 1, hi = limit;
                while (lo < hi) {
                    int mid = (lo + hi) / 2;
                    if (ext[mid] < target)
                        lo = mid + 1;
                    else
                        hi = mid;
                }
                if (lo == limit) {
                    valid = false;
                    break;
                }
                cur = lo;
                last = ext[cur];
            }
            if (valid && ext[start] + L - last >= d)
                return true;
        }
        return false;
    }
}
```
```python3 []
from bisect import bisect_left
from typing import List

class Solution:
    def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:
        n = len(points)
        pos = [0] * n
        # Map each point to a coordinate along the perimeter [0, 4*side)
        for i, (x, y) in enumerate(points):
            if y == 0:
                pos[i] = x
            elif x == side:
                pos[i] = side + y
            elif y == side:
                pos[i] = 2 * side + (side - x)
            else:  # x == 0
                pos[i] = 3 * side + (side - y)
        pos.sort()
        
        L = 4 * side
        # Build an "extended" list to simulate wrap‐around.
        ext = pos + [p + L for p in pos]

        def canPlace(d: int) -> bool:
            # Try every starting index
            for start in range(n):
                cur = start
                last = ext[start]
                valid = True
                limit = start + n  # We only consider ext indices in [start, start+n)
                for _ in range(1, k):
                    target = last + d
                    # Look for the first index in ext[cur+1:limit] with value >= target.
                    lo = bisect_left(ext, target, cur + 1, limit)
                    if lo == limit:
                        valid = False
                        break
                    last = ext[lo]
                    cur = lo
                # Check that the wrap‐around gap (from ext[start] + L to last) is at least d.
                if valid and (ext[start] + L - last) >= d:
                    return True
            return False
        
        low, high = 0, 2 * side
        while low < high:
            mid = (low + high + 1) // 2
            if canPlace(mid):
                low = mid
            else:
                high = mid - 1
        return low
```
```c []
#include <stdio.h>
#include <stdlib.h>

int cmpLongLong(const void* a, const void* b) {
    long long valA = *(long long*)a;
    long long valB = *(long long*)b;
    if (valA < valB) return -1;
    else if (valA > valB) return 1;
    else return 0;
}

int canPlace(long long d, int k, int n, long long* ext, long long L) {
    for (int start = 0; start < n; start++) {
        int cur = start;
        long long last = ext[start];
        int valid = 1;
        int limit = start + n;
        for (int step = 1; step < k; step++) {
            long long target = last + d;
            int lo = cur + 1, hi = limit;
            while (lo < hi) {
                int mid = (lo + hi) / 2;
                if (ext[mid] < target)
                    lo = mid + 1;
                else
                    hi = mid;
            }
            if (lo == limit) {
                valid = 0;
                break;
            }
            cur = lo;
            last = ext[cur];
        }
        if (valid && (ext[start] + L - last >= d))
            return 1;
    }
    return 0;
}

int maxDistance(int side, int** points, int pointsSize, int* pointsColSize, int k) {
    int n = pointsSize;
    long long* pos = (long long*) malloc(n * sizeof(long long));
    for (int i = 0; i < n; i++) {
        int x = points[i][0], y = points[i][1];
        long long p;
        if (y == 0) {
            p = x;
        } else if (x == side) {
            p = side + y;
        } else if (y == side) {
            p = 2LL * side + (side - x);
        } else {
            p = 3LL * side + (side - y);
        }
        pos[i] = p;
    }
    qsort(pos, n, sizeof(long long), cmpLongLong);
    
    long long L = 4LL * side;
    int total = n * 2;
    long long* ext = (long long*) malloc(total * sizeof(long long));
    for (int i = 0; i < n; i++) {
        ext[i] = pos[i];
        ext[i+n] = pos[i] + L;
    }
    
    long long low = 0, high = 2LL * side;
    while (low < high) {
        long long mid = (low + high + 1) / 2;
        if (canPlace(mid, k, n, ext, L))
            low = mid;
        else
            high = mid - 1;
    }
    
    free(pos);
    free(ext);
    return (int) low;
}
```
```csharp []
using System;

public class Solution {
    public int MaxDistance(int side, int[][] points, int k) {
        int n = points.Length;
        long[] pos = new long[n];
        for (int i = 0; i < n; i++) {
            int x = points[i][0], y = points[i][1];
            long p;
            if (y == 0)
                p = x;
            else if (x == side)
                p = side + y;
            else if (y == side)
                p = 2L * side + (side - x);
            else
                p = 3L * side + (side - y);
            pos[i] = p;
        }
        Array.Sort(pos);
        long L = 4L * side;
        int total = n * 2;
        long[] ext = new long[total];
        for (int i = 0; i < n; i++) {
            ext[i] = pos[i];
            ext[i + n] = pos[i] + L;
        }
        
        long low = 0, high = 2L * side;
        while (low < high) {
            long mid = (low + high + 1) / 2;
            if (CanPlace(mid, k, n, ext, L))
                low = mid;
            else
                high = mid - 1;
        }
        return (int) low;
    }
    
    private bool CanPlace(long d, int k, int n, long[] ext, long L) {
        for (int start = 0; start < n; start++) {
            int cur = start;
            long last = ext[start];
            bool valid = true;
            int limit = start + n;
            for (int step = 1; step < k; step++) {
                long target = last + d;
                int lo = cur + 1, hi = limit;
                while (lo < hi) {
                    int mid = (lo + hi) / 2;
                    if (ext[mid] < target)
                        lo = mid + 1;
                    else
                        hi = mid;
                }
                if (lo == limit) {
                    valid = false;
                    break;
                }
                cur = lo;
                last = ext[cur];
            }
            if (valid && (ext[start] + L - last >= d))
                return true;
        }
        return false;
    }
}
```
```javascript []
/**
 * @param {number} side
 * @param {number[][]} points
 * @param {number} k
 * @return {number}
 */
var maxDistance = function(side, points, k) {
    const n = points.length;
    const pos = new Array(n);
    for (let i = 0; i < n; i++){
        const [x, y] = points[i];
        let p;
        if (y === 0) {
            p = x;
        } else if (x === side) {
            p = side + y;
        } else if (y === side) {
            p = 2 * side + (side - x);
        } else {
            p = 3 * side + (side - y);
        }
        pos[i] = p;
    }
    pos.sort((a, b) => a - b);
    const L = 4 * side;
    const total = n * 2;
    const ext = new Array(total);
    for (let i = 0; i < n; i++){
        ext[i] = pos[i];
        ext[i+n] = pos[i] + L;
    }
    
    const canPlace = function(d) {
        for (let start = 0; start < n; start++){
            let cur = start;
            let last = ext[start];
            let valid = true;
            const limit = start + n;
            for (let step = 1; step < k; step++){
                const target = last + d;
                let lo = cur + 1, hi = limit;
                while (lo < hi) {
                    const mid = Math.floor((lo + hi) / 2);
                    if (ext[mid] < target)
                        lo = mid + 1;
                    else
                        hi = mid;
                }
                if (lo === limit) {
                    valid = false;
                    break;
                }
                cur = lo;
                last = ext[cur];
            }
            if (valid && (ext[start] + L - last >= d))
                return true;
        }
        return false;
    }
    
    let low = 0, high = 2 * side;
    while (low < high) {
        const mid = Math.floor((low + high + 1) / 2);
        if (canPlace(mid))
            low = mid;
        else
            high = mid - 1;
    }
    return low;
};
```
```typescript []
function maxDistance(side: number, points: number[][], k: number): number {
    const n = points.length;
    const pos: number[] = new Array(n);
    for (let i = 0; i < n; i++){
        const [x, y] = points[i];
        let p: number;
        if (y === 0) {
            p = x;
        } else if (x === side) {
            p = side + y;
        } else if (y === side) {
            p = 2 * side + (side - x);
        } else {
            p = 3 * side + (side - y);
        }
        pos[i] = p;
    }
    pos.sort((a, b) => a - b);
    const L = 4 * side;
    const total = n * 2;
    const ext: number[] = new Array(total);
    for (let i = 0; i < n; i++){
        ext[i] = pos[i];
        ext[i+n] = pos[i] + L;
    }
    
    const canPlace = (d: number): boolean => {
        for (let start = 0; start < n; start++){
            let cur = start;
            let last = ext[start];
            let valid = true;
            const limit = start + n;
            for (let step = 1; step < k; step++){
                const target = last + d;
                let lo = cur + 1, hi = limit;
                while (lo < hi) {
                    const mid = Math.floor((lo + hi) / 2);
                    if (ext[mid] < target)
                        lo = mid + 1;
                    else
                        hi = mid;
                }
                if (lo === limit) {
                    valid = false;
                    break;
                }
                cur = lo;
                last = ext[cur];
            }
            if (valid && (ext[start] + L - last >= d)) return true;
        }
        return false;
    };
    
    let low = 0, high = 2 * side;
    while (low < high) {
        const mid = Math.floor((low + high + 1) / 2);
        if (canPlace(mid))
            low = mid;
        else
            high = mid - 1;
    }
    return low;
}
```
```php []
class Solution {
    /**
     * @param Integer $side
     * @param Integer[][] $points
     * @param Integer $k
     * @return Integer
     */
    function maxDistance($side, $points, $k) {
        $n = count($points);
        $pos = array_fill(0, $n, 0);
        for ($i = 0; $i < $n; $i++) {
            list($x, $y) = $points[$i];
            if ($y == 0)
                $p = $x;
            else if ($x == $side)
                $p = $side + $y;
            else if ($y == $side)
                $p = 2 * $side + ($side - $x);
            else
                $p = 3 * $side + ($side - $y);
            $pos[$i] = $p;
        }
        sort($pos, SORT_NUMERIC);
        $L = 4 * $side;
        $ext = array_merge($pos, array_map(function($p) use ($L) { return $p + $L; }, $pos));
        
        $canPlace = function($d) use ($ext, $n, $L, $k) {
            for ($start = 0; $start < $n; $start++) {
                $cur = $start;
                $last = $ext[$start];
                $valid = true;
                $limit = $start + $n;
                for ($step = 1; $step < $k; $step++) {
                    $target = $last + $d;
                    $lo = $cur + 1;
                    $hi = $limit;
                    while ($lo < $hi) {
                        $mid = intdiv($lo + $hi, 2);
                        if ($ext[$mid] < $target)
                            $lo = $mid + 1;
                        else
                            $hi = $mid;
                    }
                    if ($lo == $limit) {
                        $valid = false;
                        break;
                    }
                    $cur = $lo;
                    $last = $ext[$cur];
                }
                if ($valid && ($ext[$start] + $L - $last >= $d))
                    return true;
            }
            return false;
        };
        
        $low = 0;
        $high = 2 * $side;
        while ($low < $high) {
            $mid = intdiv($low + $high + 1, 2);
            if ($canPlace($mid))
                $low = $mid;
            else
                $high = $mid - 1;
        }
        return $low;
    }
}
```
```swift []
class Solution {
    func maxDistance(_ side: Int, _ points: [[Int]], _ k: Int) -> Int {
        let n = points.count
        var pos = [Int](repeating: 0, count: n)
        for i in 0..<n {
            let x = points[i][0], y = points[i][1]
            let p: Int
            if y == 0 {
                p = x
            } else if x == side {
                p = side + y
            } else if y == side {
                p = 2 * side + (side - x)
            } else {
                p = 3 * side + (side - y)
            }
            pos[i] = p
        }
        pos.sort()
        let L = 4 * side
        let ext = pos + pos.map { $0 + L }
        
        func canPlace(_ d: Int) -> Bool {
            for start in 0..<n {
                var cur = start
                var last = ext[start]
                let limit = start + n
                var valid = true
                for _ in 1..<k {
                    let target = last + d
                    var lo = cur + 1
                    var hi = limit
                    while lo < hi {
                        let mid = (lo + hi) / 2
                        if ext[mid] < target {
                            lo = mid + 1
                        } else {
                            hi = mid
                        }
                    }
                    if lo == limit {
                        valid = false
                        break
                    }
                    cur = lo
                    last = ext[cur]
                }
                if valid && (ext[start] + L - last >= d) {
                    return true
                }
            }
            return false
        }
        
        var low = 0, high = 2 * side
        while low < high {
            let mid = (low + high + 1) / 2
            if canPlace(mid) {
                low = mid
            } else {
                high = mid - 1
            }
        }
        return low
    }
}
```
```dart []
class Solution {
  int maxDistance(int side, List<List<int>> points, int k) {
    int n = points.length;
    List<int> pos = List.filled(n, 0);
    for (int i = 0; i < n; i++) {
      int x = points[i][0], y = points[i][1];
      int p;
      if (y == 0) {
        p = x;
      } else if (x == side) {
        p = side + y;
      } else if (y == side) {
        p = 2 * side + (side - x);
      } else {
        p = 3 * side + (side - y);
      }
      pos[i] = p;
    }
    pos.sort();
    int L = 4 * side;
    List<int> ext = [...pos, ...pos.map((p) => p + L)];
    
    bool canPlace(int d) {
      for (int start = 0; start < n; start++) {
        int cur = start;
        int last = ext[start];
        bool valid = true;
        int limit = start + n;
        for (int step = 1; step < k; step++) {
          int target = last + d;
          int lo = cur + 1, hi = limit;
          while (lo < hi) {
            int mid = (lo + hi) >> 1;
            if (ext[mid] < target) {
              lo = mid + 1;
            } else {
              hi = mid;
            }
          }
          if (lo == limit) {
            valid = false;
            break;
          }
          cur = lo;
          last = ext[cur];
        }
        if (valid && (ext[start] + L - last >= d)) return true;
      }
      return false;
    }
    
    int low = 0, high = 2 * side;
    while (low < high) {
      int mid = (low + high + 1) >> 1;
      if (canPlace(mid)) {
        low = mid;
      } else {
        high = mid - 1;
      }
    }
    return low;
  }
}
```
```go []
package main

import (
    "sort"
)

func maxDistance(side int, points [][]int, k int) int {
    n := len(points)
    pos := make([]int, n)
    for i := 0; i < n; i++ {
        x, y := points[i][0], points[i][1]
        var p int
        if y == 0 {
            p = x
        } else if x == side {
            p = side + y
        } else if y == side {
            p = 2*side + (side - x)
        } else {
            p = 3*side + (side - y)
        }
        pos[i] = p
    }
    sort.Ints(pos)
    L := 4 * side
    ext := make([]int, 2*n)
    for i := 0; i < n; i++ {
        ext[i] = pos[i]
        ext[i+n] = pos[i] + L
    }
    
    canPlace := func(d int) bool {
        for start := 0; start < n; start++ {
            cur := start
            last := ext[start]
            valid := true
            limit := start + n
            for step := 1; step < k; step++ {
                target := last + d
                lo, hi := cur+1, limit
                for lo < hi {
                    mid := (lo + hi) / 2
                    if ext[mid] < target {
                        lo = mid + 1
                    } else {
                        hi = mid
                    }
                }
                if lo == limit {
                    valid = false
                    break
                }
                cur = lo
                last = ext[cur]
            }
            if valid && (ext[start]+L-last >= d) {
                return true
            }
        }
        return false
    }
    
    low, high := 0, 2*side
    for low < high {
        mid := (low + high + 1) / 2
        if canPlace(mid) {
            low = mid
        } else {
            high = mid - 1
        }
    }
    return low
}
```
```rust []
impl Solution {
    pub fn max_distance(side: i32, points: Vec<Vec<i32>>, k: i32) -> i32 {
        let n = points.len();
        let side64 = side as i64;
        let mut pos: Vec<i64> = Vec::with_capacity(n);
        
        // Map each point to its perimeter coordinate as an i64.
        for p in points {
            let x = p[0] as i64;
            let y = p[1] as i64;
            let coordinate = if y == 0 {
                x
            } else if x == side64 {
                side64 + y
            } else if y == side64 {
                2 * side64 + (side64 - x)
            } else {  // x == 0
                3 * side64 + (side64 - y)
            };
            pos.push(coordinate);
        }
        pos.sort_unstable();
        
        let L = 4 * side64;
        // Build the extended array.
        let mut ext: Vec<i64> = Vec::with_capacity(2 * n);
        ext.extend_from_slice(&pos);
        for &p in &pos {
            ext.push(p + L);
        }
        
        let n_usize = n;  // n as usize.
        let k_usize = k as usize;
        
        // Define a closure that checks if we can place k points with gap >= d.
        let can_place = |d: i64| -> bool {
            for start in 0..n_usize {
                let limit = start + n_usize;
                let mut cur = start;
                let mut last = ext[start];
                let mut valid = true;
                // Try to place k-1 additional points.
                for _ in 1..k_usize {
                    let target = last + d;
                    // Search in the current slice using partition_point.
                    let slice = &ext[cur + 1..limit];
                    let pos_in_slice = slice.partition_point(|&x| x < target);
                    if pos_in_slice == slice.len() {
                        valid = false;
                        break;
                    }
                    cur = cur + 1 + pos_in_slice;
                    last = ext[cur];
                }
                if valid && (ext[start] + L - last) >= d {
                    return true;
                }
            }
            false
        };
        
        let mut low = 0;
        let mut high = 2 * side64;
        while low < high {
            let mid = (low + high + 1) / 2;
            if can_place(mid) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        low as i32
    }
}
```
['Swift', 'C', 'Java', 'Go', 'TypeScript', 'Python3', 'Rust', 'JavaScript', 'C#', 'Dart']