You are given an integer array arr
, an integer target
, and an integer start
representing an index in arr
.
Your task is to find the minimum distance from start
to any index i
such that arr[i] == target
. The distance between two indices i
and j
is defined as |i - j|
(the absolute difference).
It is guaranteed that target
exists in arr
.
Key constraints:
target
in arr
.arr = [1,2,3,4,5,1]
, target = 1
, and start = 3
, the minimum distance to any 1
from index 3
is 2
(since arr[0] = 1
and |3-0| = 3
, arr[5] = 1
and |3-5| = 2
).
To solve this problem, the first idea that comes to mind is to check every index in arr
to see if it matches the target
. For each match, calculate the distance from start
and keep track of the smallest distance found.
This is a straightforward brute-force approach, but for small arrays or a single occurrence of target
, it is efficient enough. However, for larger arrays, we might look for optimizations.
Since the array is not sorted and the only way to know where target
occurs is to check each element, there is no way to avoid scanning the array. However, as soon as we find a target
at a certain distance, we can compare it to our current minimum and update if necessary.
The problem is simple enough that the brute-force approach is actually optimal, as we have to check all potential candidates to ensure the minimum is found.
Let's break down the steps to solve the problem:
min_distance
to a large value (e.g., the length of the array).i
in arr
:
arr[i]
equals target
:
abs(i - start)
.min_distance
, update min_distance
.min_distance
.Design Justification:
target
, this is the best possible approach given the constraints.
Sample Input:
arr = [4, 2, 3, 4, 5, 4], target = 4, start = 2
Step-by-step:
min_distance = 6
(length of arr
).arr[0] = 4
(matches target)
|0 - 2| = 2
min_distance = 2
arr[1] = 2
(not target)arr[2] = 3
(not target)arr[3] = 4
(matches target)
|3 - 2| = 1
min_distance = 1
arr[4] = 5
(not target)arr[5] = 4
(matches target)
|5 - 2| = 3
min_distance
remains 11
(between index 2 and 3).
Brute-force Approach:
O(n)
, where n
is the length of arr
. We must check each element once.O(1)
, as we only use a variable to store the minimum distance.target
values are located without scanning the array.The problem asks us to find the minimum distance from a given start index to any occurrence of a target value in an array. By iterating through the array and tracking the smallest distance found, we arrive at the answer efficiently. The approach is straightforward, leverages only basic looping and comparison, and is optimal for this scenario. This makes the solution both elegant and easy to understand for programmers at any level.
class Solution:
def getMinDistance(self, arr, target, start):
min_distance = len(arr)
for i, val in enumerate(arr):
if val == target:
dist = abs(i - start)
if dist < min_distance:
min_distance = dist
return min_distance
class Solution {
public:
int getMinDistance(vector<int>& arr, int target, int start) {
int min_distance = arr.size();
for (int i = 0; i < arr.size(); ++i) {
if (arr[i] == target) {
int dist = abs(i - start);
if (dist < min_distance) {
min_distance = dist;
}
}
}
return min_distance;
}
};
class Solution {
public int getMinDistance(int[] arr, int target, int start) {
int min_distance = arr.length;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
int dist = Math.abs(i - start);
if (dist < min_distance) {
min_distance = dist;
}
}
}
return min_distance;
}
}
var getMinDistance = function(arr, target, start) {
let min_distance = arr.length;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
let dist = Math.abs(i - start);
if (dist < min_distance) {
min_distance = dist;
}
}
}
return min_distance;
};