K Closest Numbers

ARRAY
BINARY SEARCH

Problem

Given a sorted array arr of integers, an integer k, and a target integer target, find the k closest integers to target in arr. Return the k integers in ascending order. If there is a tie, prefer smaller numbers.

Examples:

findKClosestNumbers([1, 2, 3, 4, 5], 4, 3) // [1, 2, 3, 4] findKClosestNumbers([2, 4, 5, 6, 9], 3, 6) // [4, 5, 6]
Loading...