package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/find-k-closest-elements/
 */
public class Problem658 {
    public List<Integer> findClosestElements(List<Integer> arr, int k, int x) {
        // TODO
        return new ArrayList<>();
    }

    public static void main(String[] args) {
        Problem658 prob = new Problem658();
        System.out.println(prob.findClosestElements(Arrays.asList(1, 2, 3, 4, 5), 4, 3)); // [1,2,3,4]
        System.out.println(prob.findClosestElements(Arrays.asList(1, 2, 3, 4, 5), 4, -1)); // [1,2,3,4]
        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 31, 35), 4, 30)); // [20,30,31,35]
    }
}

@@ -65,16 +65,46 @@
 Arrays;%0A
+import java.util.Collections;%0A
 import j
@@ -303,464 +303,1579 @@
     
-// TODO%0A        return new ArrayList%3C%3E();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem658 prob = new Problem658();%0A        System.out.println(prob.findClosestElements(Arrays.asList(1, 2, 3, 4, 5), 4, 3)); // %5B1,2,3,4%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(1, 2, 3, 4, 5), 4, -1)); // %5B1,2,3,4%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 31, 35), 4, 30)); // %5B20,30,31,35
+List%3CInteger%3E result = new ArrayList%3C%3E();%0A        int idx = Collections.binarySearch(arr, x);%0A        if (idx %3C 0) %7B%0A            idx = (-idx) - 1;%0A        %7D%0A        int left = idx - 1;%0A        int right = idx;%0A        while (k %3E 0) %7B%0A            int diff1 = Integer.MAX_VALUE;%0A            if (left %3E= 0) %7B%0A                diff1 = x - arr.get(left);%0A            %7D%0A            int diff2 = Integer.MAX_VALUE;%0A            if (right %3C arr.size()) %7B%0A                diff2 = arr.get(right) - x;%0A            %7D%0A            if (diff1 %3C= diff2) %7B%0A                left--;%0A            %7D else %7B%0A                right++;%0A            %7D%0A            k--;%0A        %7D%0A        left++;%0A        return arr.subList(left, right);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem658 prob = new Problem658();%0A        System.out.println(prob.findClosestElements(Arrays.asList(1, 2, 3, 4, 5), 4, 3)); // %5B1,2,3,4%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(1, 2, 3, 4, 5), 4, -1)); // %5B1,2,3,4%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 31, 35), 4, 30)); // %5B20,30,31,35%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 32, 35), 4, 31)); // %5B20,30,32,35%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 32, 100), 4, 31)); // %5B10,20,30,32%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 100, 200), 3, 31)); // %5B10,20,30%5D%0A        System.out.println(prob.findClosestElements(Arrays.asList(10, 20, 30, 35, 36, 37), 3, 31)); // %5B30,35,36
 %5D%0A  

Update problem 658