Skip to content
← All posts

Minimum Distance to the Target Element: Linear Scan

4 min read
leetcodeproblemeasyarrays

LeetCode 1848, Minimum Distance to the Target Element, gives you an integer array nums, an integer target, and an integer start. You need to find the minimum value of abs(i - start) where nums[i] == target. The problem guarantees that target exists somewhere in the array.

Examples:

  • nums = [1, 2, 3, 4, 5], target = 5, start = 3 returns 1 (target is at index 4, distance is |4 - 3| = 1)
  • nums = [1], target = 1, start = 0 returns 0 (target is at index 0, distance is |0 - 0| = 0)
1i=02i=13i=24i=35i=4start=3target=5|4 - 3| = 1Answer: 1
nums = [1, 2, 3, 4, 5], target = 5, start = 3. The target value 5 is at index 4. The distance is |4 - 3| = 1.

Why this problem matters

This problem teaches you the core mechanic behind many array search problems: scanning every element while tracking a running best answer. The same "scan and update" pattern appears in problems like finding a minimum, finding the closest value to a target, or locating the nearest element that satisfies a condition. Once you internalize this loop structure, you can apply it to dozens of similar problems without hesitation.

The key insight

You do not need to sort the array or use any clever data structure. Since target could appear at multiple indices, you simply walk through every index, check if the value matches target, and if it does, compute the distance to start. You keep a running minimum of all such distances. One pass through the array is all it takes.

The solution

def get_min_distance(nums, target, start):
    min_dist = float('inf')
    for i in range(len(nums)):
        if nums[i] == target:
            min_dist = min(min_dist, abs(i - start))
    return min_dist

The loop visits every index once. When it finds a match, it calculates abs(i - start) and updates min_dist if this distance is smaller than the current best. Since the problem guarantees at least one match exists, min_dist will always be updated at least once before the function returns.

You can short-circuit and return immediately if you ever find a distance of 0 (meaning nums[start] == target). This is a minor optimization that does not change the worst-case complexity but can save time on some inputs.

Visual walkthrough

Step 1: i = 0, nums[0] = 5

5i=03i=16i=2 (start)5i=32i=4min_dist = 2

nums[0] = 5, matches target. Distance = |0 - 2| = 2. min_dist = 2.

Step 2: i = 1, nums[1] = 3

5i=03i=16i=2 (start)5i=32i=4min_dist = 2

nums[1] = 3, no match. Skip. min_dist stays 2.

Step 3: i = 2, nums[2] = 6

5i=03i=16i=2 (start)5i=32i=4min_dist = 2

nums[2] = 6, no match. Skip. min_dist stays 2.

Step 4: i = 3, nums[3] = 5

5i=03i=16i=2 (start)5i=32i=4min_dist = 1

nums[3] = 5, matches target. Distance = |3 - 2| = 1. min_dist updated to 1.

Step 5: i = 4, nums[4] = 2

5i=03i=16i=2 (start)5i=32i=4min_dist = 1

nums[4] = 2, no match. Skip. min_dist stays 1. Final answer: 1.

Complexity analysis

ApproachTimeSpace
Linear ScanO(n)O(1)

The loop runs through all n elements exactly once. The only extra variable is min_dist, so space usage is constant. There is no way to do better than O(n) in general because you need to check every element to be sure you have not missed a closer match.

The building blocks

1. Linear scan with conditional update

The foundation of this problem is iterating through an array and updating a variable only when a condition is met. Here, the condition is nums[i] == target. This "conditional accumulation" pattern is the same one you use in problems like finding the maximum element, counting occurrences, or filtering values.

2. Tracking a running minimum

Instead of collecting all distances into a list and then finding the minimum, you maintain a single variable that holds the best answer seen so far. Each time you find a candidate, you compare it against the current best and keep the smaller one. This avoids extra memory and gives you the answer as soon as the loop finishes.

Edge cases

  • Target is at the start index. The distance is 0 and you can return immediately.
  • Multiple occurrences of target. The scan considers all of them and keeps the closest.
  • Target only appears once. The scan still works, just with a single distance calculation.
  • Array of length 1. The single element must be the target (guaranteed), so the answer is |0 - start|, which is always 0 since start must be 0.
  • Target at both ends of the array. The scan compares distances from both ends to start and picks the smaller one.

From understanding to recall

This problem is simple to understand on first read. The danger is that its simplicity makes you skip practice. But the "scan and track minimum" pattern is a building block you will reach for in harder problems, and the details matter: initializing min_dist to infinity, using abs() for distance, and checking equality before computing distance. Spaced repetition drills these small decisions until they are automatic. When a harder problem requires this same loop skeleton, you will write it without pausing to think.

Related posts

CodeBricks helps you internalize patterns like this through active recall and spaced repetition. Instead of passively reading solutions, you practice writing them from scratch at scientifically optimized intervals, so the pattern sticks for good.