Skip to content

Instantly share code, notes, and snippets.

@optimistiks
Created April 25, 2023 14:28
Show Gist options
  • Select an option

  • Save optimistiks/dbe5ff86c9806ec19b41973247c13467 to your computer and use it in GitHub Desktop.

Select an option

Save optimistiks/dbe5ff86c9806ec19b41973247c13467 to your computer and use it in GitHub Desktop.
Given a sorted integer array nums and two integers—k and num—return the k closest integers to num in this array. Ensure that the result is sorted in ascending order.
export function findClosestElements(nums, k, num) {
// we are going to be searching for windows
// this range indicates elements that can be first elements in a window
// hence the last element in the range is length-k
// so if a window starts from a last element in the range,
// the window does not go out of bounds
let searchRangeFirstElementIndex = 0;
let searchRangeLastElementIndex = nums.length - k;
while (searchRangeFirstElementIndex < searchRangeLastElementIndex) {
// take our starting element of the first window as mid of the search range
const windowFirstElementIndex =
Math.floor(
(searchRangeLastElementIndex - searchRangeFirstElementIndex) / 2
) + searchRangeFirstElementIndex;
// get index of the first element that is outside of the window to the right
const windowOutsideElementIndex = windowFirstElementIndex + k;
// distance between target element and the start of the window
const distanceOfNumToStart = num - nums[windowFirstElementIndex];
// distance between target element and element outside of the window
const distanceOfNumToOutside = nums[windowOutsideElementIndex] - num;
if (distanceOfNumToOutside < distanceOfNumToStart) {
searchRangeFirstElementIndex = windowFirstElementIndex + 1;
} else {
searchRangeLastElementIndex = windowFirstElementIndex;
}
}
return nums.slice(
searchRangeFirstElementIndex,
searchRangeFirstElementIndex + k
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment