package leetcode;

/**
 * https://leetcode.com/problems/find-the-distance-value-between-two-arrays/
 */
public class Problem1385 {
    public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1385 prob = new Problem1385();
        System.out.println(prob.findTheDistanceValue(new int[]{4,5,8}, new int[]{10,9,1,8}, 2)); // 2
        System.out.println(prob.findTheDistanceValue(new int[]{1,4,2,3}, new int[]{-4,-3,6,10,20,30}, 3)); // 2
        System.out.println(prob.findTheDistanceValue(new int[]{2,1,100,3}, new int[]{-5,-2,10,-3,7}, 6)); // 1
    }
}

@@ -12,16 +12,43 @@
 tcode;%0A%0A
+import java.util.TreeSet;%0A%0A
 /**%0A * h
@@ -232,82 +232,160 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args
+TreeSet%3CInteger%3E set = new TreeSet%3C%3E();%0A        for (int a : arr2) %7B%0A            set.add(a);%0A        %7D%0A        int answer = 0;%0A        for (int n : arr1
 ) %7B%0A
@@ -396,370 +396,463 @@
     
-Problem1385 prob = new Problem1385();%0A        System.out.println(prob.findTheDistanceValue(new int%5B%5D%7B4,5,8%7D, new int%5B%5D%7B10,9,1,8%7D, 2)); // 2%0A        System.out.println(prob.findTheDistanceValue(new int%5B%5D%7B1,4,2,3%7D, new int%5B%5D%7B-4,-3,6,10,20,30%7D, 3)); // 2%0A        System.out.println(prob.findTheDistanceValue(new int%5B%5D%7B2,1,100,3%7D, new int%5B%5D%7B-5,-2,10,-3,7%7D, 6)); // 1
+    Integer higher = set.ceiling(n);%0A            Integer lower = set.floor(n);%0A            int diff;%0A            if (higher == null) %7B%0A                diff = Math.abs(lower - n);%0A            %7D else if (lower == null) %7B%0A                diff = Math.abs(higher - n);%0A            %7D else %7B%0A                diff = Math.min(higher - n, n - lower);%0A            %7D%0A            if (diff %3E d) %7B%0A                answer++;%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1385