Minimum Distance to the Target Element (LeetCode 1848): Solution and Explanation
Given an array, a target value and a start index, return the smallest distance from start to any index that holds the target.
Intuition
The answer is the smallest abs(i - start) over every index i with nums[i] == target. A plain scan of the whole array with a running minimum already solves it.
There is a neater order to look in. Distance 0 is only start itself, distance 1 is start - 1 and start + 1, distance 2 is the next pair out, and so on.
If you check indices in that order, the first match you see is guaranteed to be the closest, because every nearer index was checked before it. You can stop right there.
Approach: Expand outward from start
- Loop
dfrom 0 up ton - 1. - If
start - dis inside the array and holdstarget, returnd. - If
start + dis inside the array and holdstarget, returnd. - The target is guaranteed to exist, so the loop always returns. A final
return -1only keeps compilers happy.
Step-by-step example
nums = [1,2,3,4,5], target = 5, start = 3
- d = 0: index 3 holds 4, not 5.
- d = 1: the left index 2 holds 3, not 5.
- d = 1: the right index 4 holds 5. It matches.
- Every index within distance 0 was already ruled out, so return 1.
Code: Python, Java, C++
1class Solution:
2 def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
3 n = len(nums)
4 for d in range(n):
5 if start - d >= 0 and nums[start - d] == target:
6 return d
7 if start + d < n and nums[start + d] == target:
8 return d
9 return -11class Solution {
2 public int getMinDistance(int[] nums, int target, int start) {
3 int n = nums.length;
4 for (int d = 0; d < n; d++) {
5 if (start - d >= 0 && nums[start - d] == target) {
6 return d;
7 }
8 if (start + d < n && nums[start + d] == target) {
9 return d;
10 }
11 }
12 return -1;
13 }
14}1class Solution {
2public:
3 int getMinDistance(vector<int>& nums, int target, int start) {
4 int n = nums.size();
5 for (int d = 0; d < n; d++) {
6 if (start - d >= 0 && nums[start - d] == target) {
7 return d;
8 }
9 if (start + d < n && nums[start + d] == target) {
10 return d;
11 }
12 }
13 return -1;
14 }
15};Time and space complexity
O(n)
Each index is checked at most once. The search stops at the first match, so a nearby target finishes early.
O(1)
Only the loop counter is stored.
Other approaches
Full scan with a running minimum (time O(n), space O(1))
Visit every index, and for each match update best = min(best, abs(i - start)). Simple and also O(n), but it never stops early.
Common mistakes
- Stopping when only one side runs off the array. The other side may still hold the target, so keep going while either index is valid.
- Returning the index
iinstead of the distanceabs(i - start). - Forgetting that
startitself can hold the target, which makes the answer 0. - Reading
nums[start - d]before checkingstart - d >= 0, which goes out of bounds in most languages.
Frequently asked questions
Does it matter whether the left or right side is checked first?
No. Both indices at radius d are the same distance away, so whichever matches first gives the same answer d.
Why is the first match the minimum?
The radius grows by one each round, and both indices at every smaller radius were already checked and did not match.
Is the expanding search faster than a full scan?
In the worst case both are O(n). The expanding search stops as soon as it finds the target, so it is faster when the target is near start.