Problem

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.

Examples

Example 1

1
2
3
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

1
2
3
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

1
2
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] <= 10^4
  • 0 <= start < nums.length
  • target is in nums.

Solution

Method 1 – Linear Scan 1

Intuition

The key idea is to check every index in the array and find the one where the value equals the target and the distance to the start index is minimized. Since the array is small, a simple scan is efficient.

Approach

  1. Initialize a variable to store the minimum distance (set to a large value initially).
  2. Iterate through the array.
  3. For each index, if the value equals the target, calculate the absolute difference with the start index.
  4. Update the minimum distance if the current difference is smaller.
  5. Return the minimum distance found.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
public:
    int getMinDistance(vector<int>& nums, int target, int start) {
        int ans = nums.size();
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == target) {
                int d = abs(i - start);
                if (d < ans) ans = d;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func getMinDistance(nums []int, target int, start int) int {
    ans := len(nums)
    for i, v := range nums {
        if v == target {
            d := i - start
            if d < 0 { d = -d }
            if d < ans { ans = d }
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public int getMinDistance(int[] nums, int target, int start) {
        int ans = nums.length;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                int d = Math.abs(i - start);
                if (d < ans) ans = d;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun getMinDistance(nums: IntArray, target: Int, start: Int): Int {
        var ans = nums.size
        for (i in nums.indices) {
            if (nums[i] == target) {
                val d = kotlin.math.abs(i - start)
                if (d < ans) ans = d
            }
        }
        return ans
    }
}
1
2
3
4
5
6
7
8
def getMinDistance(nums: list[int], target: int, start: int) -> int:
    ans = len(nums)
    for i, v in enumerate(nums):
        if v == target:
            d = abs(i - start)
            if d < ans:
                ans = d
    return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
impl Solution {
    pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 {
        let mut ans = nums.len() as i32;
        for (i, &v) in nums.iter().enumerate() {
            if v == target {
                let d = (i as i32 - start).abs();
                if d < ans { ans = d; }
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    getMinDistance(nums: number[], target: number, start: number): number {
        let ans = nums.length;
        for (let i = 0; i < nums.length; i++) {
            if (nums[i] === target) {
                const d = Math.abs(i - start);
                if (d < ans) ans = d;
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of nums. We scan the array once.
  • 🧺 Space complexity: O(1), only a few variables are used for tracking the answer.