A harbor authority is auditing the buoys marking a straight shipping channel. Each buoy has a recorded position (in meters, measured along the channel from a fixed reference point), but the survey crew wrote the positions down in whatever order they walked the channel, not in position order. The channel is considered uniformly spaced if the buoys can be arranged in order along the channel so that every pair of neighboring buoys sits exactly the same distance apart (an arithmetic progression of positions). Given the recorded positions, decide whether the channel is uniformly spaced.
n, the number of buoys.n space-separated integers positions[0], positions[1], ..., positions[n-1], the recorded position of each buoy.Print YES if the buoys' positions can be arranged into a sequence with a constant gap between consecutive buoys, otherwise print NO.
2 <= n <= 100000-10000000 <= positions[i] <= 10000000Example 1
Input
4 3 5 1 7
Expected
YES
Explanation
Sorted, the positions are 1, 3, 5, 7 — every neighboring pair differs by exactly 2, so the channel is uniformly spaced and the answer is YES.
Example 2
Input
5 1 2 4 7 11
Expected
NO
Explanation
Sorted, the positions are 1, 2, 4, 7, 11 — the gaps are 1, 2, 3, 4, which are not all equal, so no arrangement gives a constant spacing and the answer is NO.
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 →