Minimum Distance to the Target Element (LeetCode 1848): Solution and Explanation

Easy
Array

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

  1. Loop d from 0 up to n - 1.
  2. If start - d is inside the array and holds target, return d.
  3. If start + d is inside the array and holds target, return d.
  4. The target is guaranteed to exist, so the loop always returns. A final return -1 only keeps compilers happy.

Step-by-step example

nums = [1,2,3,4,5], target = 5, start = 3
  1. d = 0: index 3 holds 4, not 5.
  2. d = 1: the left index 2 holds 3, not 5.
  3. d = 1: the right index 4 holds 5. It matches.
  4. 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 -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each index is checked at most once. The search stops at the first match, so a nearby target finishes early.

Space

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 i instead of the distance abs(i - start).
  • Forgetting that start itself can hold the target, which makes the answer 0.
  • Reading nums[start - d] before checking start - 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.