2D square into 1D array
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
```java []
class Solution {
    public void fillArr(long[] arr, int[][] points, int n, long side){
        HashSet<Integer> set = new HashSet<>();
        int ptr = 0;
        List<Long> list = new ArrayList<>();

        // y = 0
        for(int i = 0; i < n; i++){
            int[] point = points[i];
            if(set.contains(i)) continue;
            if(point[1] == 0) {
                list.add((long)(point[0]));
                set.add(i);
            }
        }
        Collections.sort(list);
        for(long x: list){
            arr[ptr] = x;
            ptr++;
        }
        list.clear();


        // x = side
        for(int i = 0; i < n; i++){
            int[] point = points[i];
            if(set.contains(i)) continue;
            if(point[0] == side) {
                list.add((long)(point[0]) + point[1]);
                set.add(i);
            }
        }
        Collections.sort(list);
        for(long x: list){
            arr[ptr] = x;
            ptr++;
        }
        list.clear();

        // y = side
        for(int i = 0; i < n; i++){
            int[] point = points[i];
            if(set.contains(i)) continue;
            if(point[1] == side) {
                list.add(3 * side - point[0]);
                set.add(i);
            }
        }
        Collections.sort(list);
        for(long x: list){
            arr[ptr] = x;
            ptr++;
        }
        list.clear();

        // x = 0
        for(int i = 0; i < n; i++){
            int[] point = points[i];
            if(set.contains(i)) continue;
            if(point[0] == 0) {
                list.add(4 * side - point[1]);
                set.add(i);
            }
        }
        Collections.sort(list);
        for(long x: list){
            arr[ptr] = x;
            ptr++;
        }
    }
    public boolean check(long reqMinDist, long[] arr, int n, int k, long side){
        for(int startIdx = 0; startIdx < n; startIdx++){

            if(arr[startIdx] - arr[0] >= reqMinDist) break;

            int completed = 1;
            int prevIdx = startIdx;
            for(int i = 1; i < n; i++){
                if(arr[i] - arr[prevIdx] >= reqMinDist){
                    prevIdx = i;
                    completed++;
                }
                if(completed == k){
                    long lastDist = 4 * side - arr[prevIdx] + arr[startIdx];
                    if(lastDist >= reqMinDist) return true;
                }
            }
        }
        return false;
    }
    public int maxDistance(int side, int[][] points, int k) {
        int n = points.length;
        long low = 1, high = 2 * side, ans = 1;
        long[] arr = new long[n];
        fillArr(arr, points, n, side);

        // System.out.println("Printing arr: ");
        // for(int i = 0; i < n; i++) {
        //     System.out.print(arr[i] +" ");
        // }
        // System.out.println("");

        while(low <= high){
            long mid = low + (high - low) / 2;

            boolean b = check(mid, arr, n, k, side);

            // System.out.println("low: " + low + ", high: " + high + ", mid: " + mid + ", b: " + b);

            if(b){
                ans = mid;
                low = mid + 1;
            } else{
                high = mid - 1;
            }
        }
        return (int)(ans);
    }
}
```
['Java']