LeetCode Problem Workspace
Minimum Distance to the Target Element
Solve Minimum Distance to the Target Element by scanning the array and tracking the smallest distance from start.
1
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array-driven solution strategy
Answer-first summary
Solve Minimum Distance to the Target Element by scanning the array and tracking the smallest distance from start.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array-driven solution strategy
For Minimum Distance to the Target Element, the clean solution is to scan nums, check every index where nums[i] equals target, and keep the smallest abs(i - start). Because target is guaranteed to exist, one pass is enough and there is no need for extra storage. A bidirectional walk from start also works, but the full scan is usually the simplest way to avoid missing a closer match.
Problem Statement
You are given a 0-indexed integer array nums plus two integers target and start. Your job is to find an index i where nums[i] equals target, with the distance from start to i as small as possible.
After choosing the best matching index, return that minimum absolute distance. The key detail is that target is guaranteed to appear in nums, so the problem is only about finding the closest occurrence, not checking whether one exists.
Examples
Example 1
Input: nums = [1,2,3,4,5], target = 5, start = 3
Output: 1
nums[4] = 5 is the only value equal to target, so the answer is abs(4 - 3) = 1.
Example 2
Input: nums = [1], target = 1, start = 0
Output: 0
nums[0] = 1 is the only value equal to target, so the answer is abs(0 - 0) = 0.
Example 3
Input: nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
Output: 0
Every value of nums is 1, but nums[0] minimizes abs(i - start), which is abs(0 - 0) = 0.
Constraints
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 104
- 0 <= start < nums.length
- target is in nums.
Solution Approach
One-pass array scan
Walk through every index in nums. Whenever nums[i] equals target, compute abs(i - start) and update the best answer. This fits the Array pattern directly because the only thing that matters is comparing distances across matching positions.
Bidirectional search from start
Another problem-specific option is to expand outward from start: check start, then start - 1 and start + 1, then the next layer, until target appears. This works because the first hit guarantees the minimum distance, but boundary handling is easier to get wrong than in a plain scan.
Why brute force is already optimal here
Since nums.length is at most 1000, a linear pass is both fast and simple. Preprocessing target positions or using extra data structures adds complexity without improving the real trade-off for this exact problem.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The standard scan runs in O(n) time because each array position is checked once, and it uses O(1) extra space because only the current best distance is stored. A bidirectional walk is also O(n) time and O(1) space in the worst case.
What Interviewers Usually Probe
- They want you to notice that only indices with nums[i] == target matter, not the values around them.
- A strong answer mentions that target is guaranteed to exist, so no fallback case is needed.
- If they hint at checking both directions from start, they are steering you toward an outward array search with early stop.
Common Pitfalls or Variants
Common pitfalls
- Stopping at the first target seen in a left-to-right scan can fail when a later target is closer to start.
- Forgetting absolute value leads to wrong comparisons when the target index is left of start.
- Overengineering with sorting or extra maps misses that this problem is just a direct array distance check.
Follow-up variants
- Return the closest target index instead of the distance, with a clear tie rule if both sides match.
- Remove the guarantee that target exists and return -1 when no matching value appears.
- Process many queries on the same nums, where preprocessing target indices could help repeated lookups.
FAQ
What is the easiest way to solve Minimum Distance to the Target Element?
Scan the full array, and every time nums[i] equals target, compute abs(i - start) and keep the minimum. That is the most direct and reliable solution for this problem.
Can I search outward from start instead of scanning everything?
Yes. Check start first, then move one step left and right, then two steps, and so on. The first target you hit gives the answer, but you must handle array boundaries carefully.
Why is the Array-driven solution strategy enough here?
The problem only asks for the closest matching index in one array, so a single traversal already compares every valid candidate. There is no need for dynamic programming, hashing, or sorting.
What mistake causes wrong answers most often?
A common bug is returning when you see the first target during a normal left-to-right pass. That can miss another target index that is closer to start.
What are the time and space costs for this problem?
The standard approach is O(n) time and O(1) space. You read each element once and store only the best distance found so far.
Solution
Solution 1: Single Pass
Traverse the array, find all indices equal to $target$, then calculate $|i - start|$, and take the minimum value.
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)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward