Back to problems

Find K Closest Elements

Algorithm · Visa · Medium

You are given a sorted integer array arr in ascending order, along with two integers k and x. Your task is to identify the k elements that are closest to x and return them sorted in ascending order. Closeness is determined by two rules: A smaller absolute difference a - x means a is closer. If two numbers have the same absolute difference, the smaller number is considered closer. Input (stdin) Line 1: two integers n and k Line 2: n ascending integers arr[i] Line 3: integer x…

Checking your access…