Java || 👍Explained in Detail👍|| Binary Search || Two Pointers || Priority Queue
I do my best everyday to give a clear explanation, so to help everyone improve their skills.\n\nIf you find this helpful, please \uD83D\uDC4D upvote this post a
I do my best everyday to give a clear explanation, so to help everyone improve their skills.\n\nIf you find this **helpful**, please \uD83D\uDC4D **upvote** this post and watch my [Github Repository](https://github.com/cheehwatang/leetcode-java).\n\nThank you for reading! \uD83D\uDE04 Comment if you have any questions or feedback.\n\n---\n\n**Java - Two Pointers (Clean Code)**\n```java\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n\n        int start = 0;\n        int end = arr.length - 1;\n        while (end - start >= k) {\n            if (Math.abs(arr[start] - x) > Math.abs(arr[end] - x)) {\n                start++;\n            } else {\n                end--;\n            }\n        }\n\n        List<Integer> result = new ArrayList<>(k);\n        for (int i = start; i <= end; i++) {\n            result.add(arr[i]);\n        }\n        return result;\n    }\n}\n```\n\n**Java - Two Pointers (With Explanation)**\n```\nclass Solution {\n    \n    // Approach:\n    // Using two pointers, we are going the \'start\' and \'end\' pointers towards each other,\n    // until only k elements between \'start\' and \'end\'.\n    \n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        \n        int start = 0;\n        int end = arr.length - 1;\n        // Between the \'start\' and \'end\' pointers, inclusive, contains all the k integers that is closest to x.\n        while (end - start >= k) {\n            // Move \'start\' to the right if \'end\' is closer to x, or move \'end\' to the left if \'start\' is closer to x.\n            if (Math.abs(arr[start] - x) > Math.abs(arr[end] - x)) {\n                start++;\n            } else {\n                end--;\n            }\n        }\n\n        // Input all the k closest integers into the result.\n        List<Integer> result = new ArrayList<>(k);\n        for (int i = start; i <= end; i++) {\n            result.add(arr[i]);\n        }\n        return result;\n    }\n}\n```\n\n---\n\n**Java - Binary Search (Clean Code)**\n```\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n\n        int left = 0, right = arr.length - k;\n        while (left < right) {\n            int midpoint = left + (right - left) / 2; // same as (left + right) / 2\n            if (x - arr[midpoint] > arr[midpoint + k] - x) {\n                left = midpoint + 1;\n            }\n            else {\n                right = midpoint;\n            }\n        }\n\n        List<Integer> result = new ArrayList<>(k);\n        for (int i = left; i < left + k; i++) {\n            result.add(arr[i]);\n        }\n        return result;\n    }\n}\n```\n\n**Java - Binary Search (With Explanation)**\n```\nclass Solution {\n\n    // Approach:\n    // Using binary search and a sliding window, find the midpoint where,\n    // the integers between midpoint and midpoint + k is the k closest integers to x.\n\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n\n        // The sliding window is between \'mid\' and \'mid\' + k.\n        int left = 0, right = arr.length - k;\n        while (left < right) {\n            int midpoint = left + (right - left) / 2; // same as (left + right) / 2\n\n            // With midpoint on the left, we use x - arr[midpoint], while arr[midpoint + k] - x because it is on the right.\n            // This is important!\n            // Rather than using Math.abs(), we need the direction keep the x within the sliding window.\n            // If the window is too far left, we shift the window to the right.\n            if (x - arr[midpoint] > arr[midpoint + k] - x) {\n                left = midpoint + 1;\n            }\n            // If the window is too far right, we shift the window to the left.\n            else {\n                right = midpoint;\n            }\n        }\n\n        // Input all the k closest integers into the result.\n        List<Integer> result = new ArrayList<>(k);\n        for (int i = left; i < left + k; i++) {\n            result.add(arr[i]);\n        }\n        return result;\n    }\n}\n```\n\n---\n\n**Java - Priority Queue (Clean Code)**\n```\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        PriorityQueue<Integer> minHeap = new PriorityQueue<>();\n        for (int integer : arr) {\n            if (k > 0) {\n                minHeap.offer(integer);\n                k--;\n            } else if (Math.abs(minHeap.peek() - x) > Math.abs(integer - x)) {\n                minHeap.poll();\n                minHeap.offer(integer);\n            }\n        }\n        List<Integer> result = new ArrayList<>();\n        while (!minHeap.isEmpty()) {\n            result.add(minHeap.poll());\n        }\n        return result;\n    }\n}\n```\n\n**Java - Priority Queue (With Explanation)**\n```\nclass Solution {\n\n    // Approach:\n    // Using a min heap priority queue, add all the smallest integers up to k integers.\n    // Then, traverse the \'arr\' array will replacing the priority queue with integer closer to x.\n\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        \n        PriorityQueue<Integer> minHeap = new PriorityQueue<>();\n        // Traverse the array with,\n        // First, add all the smallest integers up to k number.\n        // Second, if found a closer integer to x, remove the smallest integer from the priority queue, and add the new integer.\n        // This is because the smallest integer is always the further to x, if a larger number is closer to x.\n        for (int integer : arr) {\n            if (k > 0) {\n                minHeap.offer(integer);\n                k--;\n            } else if (Math.abs(minHeap.peek() - x) > Math.abs(integer - x)) {\n                minHeap.poll();\n                minHeap.offer(integer);\n            }\n        }\n        \n        // Add the integers from the priority queue to the result.\n        // This will automatically add in ascending order, from smallest to largest k integers closest to x.\n        List<Integer> result = new ArrayList<>();\n        while (!minHeap.isEmpty()) {\n            result.add(minHeap.poll());\n        }\n        return result;\n    }\n}\n```
['Two Pointers', 'Heap (Priority Queue)', 'Binary Tree', 'Java']