Find K Closest Elements
Try to solve the Find K Closest Elements problem.
We'll cover the following
Statement
You are given a sorted array of integers, nums
, and two integers, target
and k
. Your task is to return k
number of integers that are close to the target value, target
. The integers in the output array should be in a sorted order.
An integer, nums[i]
, is considered to be closer to target
, as compared to nums[j]
when |nums[i]
- target
| |nums[j]
- target
|. However, when |nums[i]
- target
| |nums[j]
- target
|, the smaller of the two values is selected.
Constraints:
-
k
nums.length
-
nums.length
nums
is sorted in ascending order.-
nums[i]
,target
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy