Back to problems

Find K Closest Elements in a Sorted Array (LC 658)

Algorithm · LinkedIn · Medium

You are given an array arr of integers that is already sorted in non‑decreasing order (duplicate values may appear). You are also given an integer x and a positive integer k. Your task is to identify the k elements from arr that are nearest to x. Return these elements as an array sorted in ascending order. We define “nearest” by absolute difference: an element a is nearer to x than an element b if a - x < b - x . If two elements have the same absolute difference, the element…

Checking your access…