1848. Minimum Distance to the Target Element

  • Array
TIME
O(n)
SPACE
O(1)
problem on leetcode ↗source on github ↗

Intuition

Simple linear scan to find the target value closest to the start index.

Solution

Iterate through the array. For each index where nums[i] == target, compute |i - start| and track the minimum.

min_dist = infinity
for i in range(len(nums)):
    if nums[i] == target:
        min_dist = min(min_dist, abs(i - start))

Optimization (not needed for small n)

Could expand outward from start and return immediately when target is found, but linear scan is sufficient for n ≤ 1000.

Complexity

  • Time: O(n)
  • Space: O(1)

Edge Cases

  • Target at start index → return 0
  • Single element array → return 0
  • Multiple occurrences of target → pick closest
  • Target only at one end

Code

from typing import List


class Solution:
    def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
        """
        1848. Minimum Distance to the Target Element
        Time: O(n)
        Space: O(1)
        """
        min_dist = float('inf')

        for i, num in enumerate(nums):
            if num == target:
                min_dist = min(min_dist, abs(i - start))

        return min_dist