Skip to content

Practice Minimum Distance to the Target Element

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1848
LeetCode Problem

Minimum Distance to the Target Element

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.

Source: Minimum Distance to the Target Element on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,3,4,5], target = 5, start = 3
Output
1

# Example 2

Input
nums = [1], target = 1, start = 0
Output
0

# Example 3

Input
nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
Output
0

Constraints

1 <= nums.length <= 1000
1 <= nums[i] <= 10^4
0 <= start < nums.length
target is in nums.

Related LeetCode Problems

Practice more problems similar to Minimum Distance to the Target Element.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.