Skip to main content

Minimum Distance to the Target Element - Solution & Explanation

EasyArray14 min readAsked at: Amazon, Honeywell, Google +1
Practice this problem

Problem Statement

Given an integer array nums (0-indexed) and two integers target and start, find an index i such that nums[i] == target and abs(i - start) is minimized. Note that abs(x) is the absolute value of x.

Return abs(i - start).

It is guaranteed that target exists in nums.

 

Example 1:

Input: nums = [1,2,3,4,5], target = 5, start = 3
Output: 1
Explanation: 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
Explanation: 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
Explanation: 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.

Approach Overview

Problem Overview: You are given an integer array nums, a target value, and a start index. The task is to return the minimum absolute distance between start and any index i where nums[i] == target. The array may contain multiple occurrences of the target, so you must check all candidates and choose the smallest distance.

Approach 1: Linear Search from Start (Time: O(n), Space: O(1))

The simplest solution is a full scan of the array. Iterate through every index i from 0 to n-1. Whenever nums[i] equals the target, compute the distance using abs(i - start) and keep track of the minimum value seen so far. This approach relies on a straightforward traversal of the array and constant-time comparisons. Since every element is visited once, the time complexity is O(n), while memory usage stays O(1) because only a few variables are stored. This method is reliable and easy to implement in any language.

Approach 2: Bi-directional Search (Time: O(n) worst case, Space: O(1))

A more intuitive strategy is to start from the start index and expand outward in both directions. Check positions start, start-1, start+1, start-2, start+2, and so on until the target is found. The first match encountered automatically gives the minimum distance because the search expands in increasing distance order. This pattern resembles a two-sided pointer expansion similar to techniques used in two pointer problems. In the best case, the target is adjacent to start and the algorithm returns immediately. In the worst case, the scan reaches the ends of the array, resulting in O(n) time and O(1) space.

The bidirectional technique reduces unnecessary checks when the closest target lies near the starting position. However, its worst-case performance is still linear because the algorithm may need to inspect every element.

Recommended for interviews: The linear scan is usually the expected baseline solution. It clearly demonstrates understanding of array traversal and absolute distance calculation. The bi-directional expansion is a nice optimization idea and shows problem-solving intuition. In interviews, start with the linear approach, explain its O(n) complexity, then mention the bidirectional strategy as an improvement that can terminate earlier when the closest target is near the starting index.

Approach 1: Linear Search from Start

The simplest approach is to iterate through the entire array and check for each target occurrence. Calculate the absolute difference between the current index and the start index. Keep track of the minimum difference encountered and return that value.

This C program defines a function minDistance that iterates through the array, checking each element against the target. For each match, it calculates the absolute distance and updates the minimum distance accordingly.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), as we are using a constant amount of space.

Try this approach in the editor →

Approach 2: Bi-directional Search

This approach leverages the possibility of starting two pointers, one from the start index moving forward and the other moving backward, thereby potentially reducing the number of elements to check before finding the target. The method stops early if a zero-distance index is found, as it represents the minimum possible distance.

The C implementation uses two pointers, left and right, starting from the start index. They move towards the left and right ends of the array, respectively, seeking the target.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n/2) on average, though still O(n) in the worst-case scenario.
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Single Pass

Traverse the array, find all indices equal to target, then calculate |i - start|, and take the minimum value.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Linear Search from Start

Time Complexity: O(n), where n is the length of the array.
Space Complexity: O(1), as we are using a constant amount of space.

Bi-directional Search

Time Complexity: O(n/2) on average, though still O(n) in the worst-case scenario.
Space Complexity: O(1)

Single Pass—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Linear Search from StartO(n)O(1)General solution for any array; simplest and most common interview approach
Bi-directional SearchO(n) worst caseO(1)Useful when the closest target is expected near the start index, allowing early termination

Video Solution

Minimum Distance to the Target Element | Simple Explanation | Early Break | Leetcode 1848 | MIK • codestorywithMIK • 2,614 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Distance to the Target Element easy or hard?
Minimum Distance to the Target Element is categorized as an Easy problem. It focuses on basic array traversal, condition checks, and computing absolute differences without requiring complex algorithms.
Minimum Distance to the Target Element Python/Java solution
Both Python and Java implementations follow the same logic: loop through the array, check if nums[i] equals the target, and track the minimum absolute difference from the start index. The algorithm runs in O(n) time and uses constant space.
How to solve Minimum Distance to the Target Element in O(n)?
Iterate through the array from index 0 to n-1. Whenever nums[i] equals the target, compute the distance using abs(i - start) and update the minimum value. After scanning all elements, return the smallest distance found.
What is the best approach for Minimum Distance to the Target Element?
The most common approach is a linear scan of the array. Iterate through all indices, check where nums[i] equals the target, and compute abs(i - start) to track the minimum distance. This runs in O(n) time and O(1) space and is typically the expected interview solution.
Is Minimum Distance to the Target Element asked at Google/Amazon/Meta?
This problem represents a common array traversal pattern often used in coding interviews. Variations that require computing minimum distance, scanning arrays, or expanding from a starting index frequently appear in interviews at large tech companies.
What data structure is used in Minimum Distance to the Target Element?
The problem primarily uses an array with simple iteration. No advanced data structures are required; the solution relies on sequential traversal and basic arithmetic operations.
What is the time complexity of Minimum Distance to the Target Element?
The standard solution runs in O(n) time because the algorithm may need to inspect every element in the array once. Space complexity is O(1) since only a few variables are used to track the current minimum distance.

Ready to solve this problem?

Practice Minimum Distance to the Target Element with our built-in code editor and test cases.

Practice on FleetCode