Problem Description
Given an array of integers nums, a target value, and a start index, find an index i such that nums[i] equals target and the absolute difference |i - start| is minimized. Return this minimum absolute difference.
Key Insights
- Since the target is guaranteed to exist in the array, a single linear scan is sufficient.
- As you iterate, compute the absolute difference between the current index and the start index for each occurrence of target.
- Maintain and update the minimum distance encountered during the scan.
Space and Time Complexity
Time Complexity: O(n), where n is the number of elements in nums. Space Complexity: O(1), as only a constant amount of extra space is used.
Solution
The solution involves iterating over the array to check for indices where the element equals the target. For each matching index, compute the absolute difference |i - start|. Track the minimum such difference found during the iteration. This straightforward approach leverages the guarantee that the target exists, ensuring that the algorithm is both simple and efficient.