Find K Closest Elements

Given a sorted integer array, two integers k and x, return the k closest integers to x in the array. The result should be sorted in ascending order. An element a is considered closer to x than an element b if either |a - x| < |b - x| or |a - x| == |b - x| and a < b.

Constraints

  • 1 <= k <= arr.length
  • 1 <= arr.length <= 10^4
  • arr is sorted in ascending order
  • -10^4 <= arr[i], x <= 10^4
Saving… · 3 lines
class Solution:
def findKClosestElements(self, arr, k, x) -> List[int]:
pass
Console

Click Run to execute sample tests.