Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up
Problem Submissions Solution

Find K Closest Elements

Difficulty: Medium

Acceptance: 76.00%

Points: 30.00

You are given a sorted integer array arr[], an integer k, and an integer x. Your task is to find the k closest elements to x in the array. An element a is closer to x than element b if the absolute difference |a - x| is smaller than |b - x|. If there is a tie, choose the smaller element. Return the k closest elements in ascending order.

Topics

Companies

Articles

Examples:

N/A

Constraints:
  • 1 = arr.length = 105
  • 1 = k = arr.length
  • -104 = arr[i], x = 104
  • arr is sorted in ascending order
Companies:
Microsoft
Topics:
Binary Search
Locked Content
Access Restricted: Please Login to access the code editor and test cases.