An airport ramp controller watches a straight row of parked aircraft, each continuously broadcasting a transponder code. When a particular code is flagged for inspection, the controller needs to know how many aircraft positions away the nearest aircraft broadcasting that code is from wherever they are currently standing, so they can decide the fastest way to walk over.
You are given the row of transponder codes, the flagged code, and the index of the controller's current position in the row. Find the minimum distance, measured in number of positions along the row (not any physical distance), between the controller's position and the index of some aircraft whose transponder code equals the flagged code. It is guaranteed that at least one aircraft in the row broadcasts the flagged code.
n, the number of aircraft in the row.n integers codes[0], ..., codes[n-1], the transponder codes.target and start: the flagged code and the controller's current position (0-indexed).Print a single integer: the minimum value of |i - start| over all indices i with codes[i] == target.
i satisfies codes[i] == target.Example 1
Input
5 1 2 3 4 5 5 3
Expected
1
Explanation
The controller is at index 3 (code 4). The flagged code 5 appears only at index 4, so the distance is |4 - 3| = 1.
Example 2
Input
1 1 1 0
Expected
0
Explanation
There is only one aircraft, at index 0, already broadcasting the flagged code 1, so the controller's own position matches and the distance is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →