Java O(n) Solution || Two Pointers || 97% Faster
\n\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        List<Integer> list = new ArrayList<Integer>();  // A list
```\n\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        List<Integer> list = new ArrayList<Integer>();  // A list to store the resultant subarray\n        int low = 0;                                    // A var to point at the left half of the array\n        int high = arr.length - 1;                      // A var to point at the right half of the array\n        \n        while(high - low >= k){                         // Traverse till the difference between \'high\' and \'low\' is not less than \'k\'\n            int distLow = Math.abs(arr[low] - x);       // Get the difference between \'x\' and the element at index \'low\' in \'distLow\'\n            int distHigh = Math.abs(arr[high] - x);     // Get the difference between \'x\' and the element at index \'high\' in \'distHigh\'\n            \n            if(distLow <= distHigh)                     // Now, check if \'distLow\' less than or equal \'distHigh\' or not\n                high--;                                 // If Yes, then move the right pointer i.e., decrease the value of \'high\' \n            else                                        // Else,\n                low++;                                  // Move the left pointer i.e., increase the value of \'low\'\n        }\n        while(low <= high)                              // After traversing the array, traverse another loop to get the resultant values\n            list.add(arr[low++]);                       // And, keep adding those values to the resultant list\n        \n        return list;                                    // Finally, return the resultant list\n    }\n}\n\n```
['Java']