If you are preparing for coding interviews or solving array problems on LeetCode, 1848. Minimum Distance to the Target Element is a good beginner-friendly problem. It helps you practice array traversal, absolute difference, and minimum value tracking.
In this article, you will learn:
- what the problem is asking
- the easiest way to solve it
- step-by-step explanation
- optimized Python code
- time and space complexity
- why the solution works
Problem: 1848. Minimum Distance to the Target Element
You are given:
- an integer array
nums - an integer
target - an integer
start
Your task is to find the minimum distance between start and any index i such that:
nums[i] == target
The distance between two indices is:
|i - start|
So, among all positions where the value equals target, you need to return the smallest absolute difference from start.
Example
Input
nums = [1, 2, 3, 4, 5]
target = 5
start = 3
Output
1
Why?
target = 5- In the array,
5is located at index4 start = 3- Distance =
|4 - 3| = 1
So the minimum distance is 1.
Understanding the Problem
The problem is not asking for the target value itself.
It is asking for the closest index where the target appears.
For every index in the array:
- check whether
nums[i] == target - if yes, compute the distance from
start - keep the smallest one
This is a direct and efficient solution.
Best Approach
The simplest way to solve Minimum Distance to the Target Element is:
- Traverse the array from left to right
- Whenever you find
target, calculateabs(i - start) - Store the minimum distance found so far
- Return the minimum distance at the end
Because we only scan the array once, this is an O(n) solution.
Python Solution
class Solution:
def getMinDistance(self, nums: list[int], target: int, start: int) -> int:
ans = float("inf")
for i in range(len(nums)):
if nums[i] == target:
ans = min(ans, abs(i - start))
return ans
Code Explanation
Let us break the code into small parts.
1. Initialize the answer
ans = float("inf")
We start with a very large value so that any valid distance will be smaller.
2. Loop through the array
for i in range(len(nums)):
We check every index i in nums.
3. Check if current element matches the target
if nums[i] == target:
Only the indices containing the target matter.
4. Compute the distance
abs(i - start)
This gives the absolute difference between the current index and start.
5. Update the minimum distance
ans = min(ans, abs(i - start))
If the current distance is smaller than the previous best, update it.
6. Return the final answer
return ans
After scanning the whole array, ans contains the minimum distance.
Dry Run
Let us take this example:
nums = [1, 2, 3, 4, 5]
target = 5
start = 3
Step-by-step
| Index | nums[i] | Is target? | Distance from start | Minimum so far |
|---|---|---|---|---|
| 0 | 1 | No | โ | inf |
| 1 | 2 | No | โ | inf |
| 2 | 3 | No | โ | inf |
| 3 | 4 | No | โ | inf |
| 4 | 5 | Yes | ` | 4 – 3 |
Final answer = 1
Why This Solution Works
This solution works because:
- every valid target position is checked
- the distance to
startis computed correctly using absolute difference - the smallest of all valid distances is preserved
Since no possible target index is skipped, the minimum distance found is guaranteed to be correct.
Time Complexity
Time Complexity: O(n)
We visit each element of the array once.
Space Complexity: O(1)
We only use one variable to store the answer, so extra space is constant.
Shorter Python Version
You can also write the same logic in a more compact way:
class Solution:
def getMinDistance(self, nums: list[int], target: int, start: int) -> int:
return min(abs(i - start) for i, x in enumerate(nums) if x == target)
How this version works
enumerate(nums)gives both index and valueif x == targetfilters only target positionsabs(i - start)computes the distancemin(...)returns the smallest distance
This version is concise, but the first version is often easier to understand in interviews.
Interview Tip
When solving problems like 1848. Minimum Distance to the Target Element, always look for:
- what exactly needs to be minimized or maximized
- whether a single scan is enough
- whether absolute difference is involved
This problem is a classic example of a linear scan with minimum tracking.
Common Mistakes
Here are a few mistakes beginners may make:
1. Returning index instead of distance
The problem asks for the minimum distance, not the index.
2. Forgetting absolute value
Distance must be non-negative, so use:
abs(i - start)
3. Stopping too early
Even if you find one occurrence of the target, there may be another one closer to start.
Final Thoughts
LeetCode 1848: Minimum Distance to the Target Element is a simple array problem, but it teaches an important pattern:
- scan the array
- check matching values
- compute distance
- keep the minimum
This makes it a great practice problem for beginners preparing for coding interviews.
Final recommended solution
class Solution:
def getMinDistance(self, nums: list[int], target: int, start: int) -> int:
ans = float("inf")
for i in range(len(nums)):
if nums[i] == target:
ans = min(ans, abs(i - start))
return ans