Dsa
Two Pointers
Easy
Distance Calibration Search
An automated vehicle travels along a straight track with markers at various sorted positions. Engineers need to find two markers whose distance apart is as close to a target G as possible. Return the absolute difference between the actual distance and G for the best pair.
Input Format:
Line 1: An integer G (the target distance).
Line 2: A space-separated list of sorted marker positions.
Output Format:
A single integer (the minimum absolute error |(pos[j] - pos[i]) - G|).
Example:
Input:
10
1 5 15 22
Output:
0
(Explanation: Marker 15 and 5 are exactly 10 apart. Error is 0.)
Key concepts
two_pointersoptimization
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now