Minimum Distance to Target Element Lab
In this lab, you will implement a function called getMinDistance
to find the minimum distance to a target element in an array. The function should take an integer array nums
(0-indexed) and two integers target
and start
as input, and return the minimized value of abs(i - start)
where nums[i] == target
. Note that abs(x)
is the absolute value of x.
It is guaranteed that target
exists in nums
.
Function Signature
/** * @param {number[]} nums * @param {number} target * @param {number} start * @return {number} */ export function getMinDistance(nums, target, start) { }
Examples
Example 1:
Input: nums = [1,2,3,4,5], target = 5, start = 3
Output: 1
Explanation: nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.
Example 2:
Input: nums = [1], target = 1, start = 0
Output: 0
Explanation: nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.
Example 3:
Input: nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
Output: 0
Explanation: Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 104
0 <= start < nums.length
target
is innums
.
Make sure to export any function, variable, or class you want to test in the evaluation script. The order of try-catch blocks inside the evaluation script must match the order of the challenges.