No.406 The Law of Equally Spaced Ducks
Problem Statement
Taro found $N (\ge 3)$ ducks resting on a riverbank near his home. This riverbank is straight. So, taking a certain point upstream as the origin $x = 0$, he represents the position where each duck is resting as a point on the part of the $x$-axis satisfying $x\ge 0$. Then, these $N$ ducks are said to satisfy the Law of Equally Spaced Ducks if and only if the following two conditions hold simultaneously.
- The coordinates of any two ducks are distinct.
- The differences between the coordinates of adjacent pairs of ducks are all equal.
Input
$N$
$x_0$ $x_1$ $\cdots$ $x_{N - 1}$
The first line contains the number of ducks $N\:(3 \le N \le 10 ^ 5$) that Taro found, as a positive integer.
The second line contains the coordinates $x_i\:(0 \le x_i \le 10 ^ 8)$ where duck $i (0 \le i \le N - 1)$ is resting, as non-negative integers separated by spaces. Note that it is not necessarily the case that "if $j \lt k$ then $x_j \lt x_k$".
Output
Print the string YES if the $N$ ducks satisfy the Law of Equally Spaced Ducks, and the string NO otherwise. Print a newline at the end.
Sample
Sample 1
Input
3 12 0 6
Output
YES
Whichever two ducks are chosen, their coordinates are different. Also, the differences between the coordinates of adjacent pairs of ducks are 6 - 0 and 12 - 6, both of which are 6. Therefore, both conditions hold, so the Law of Equally Spaced Ducks is satisfied.
Sample 2
Input
5 63 17 99 34 51
Output
NO
Taking the differences between the coordinates of adjacent pairs of ducks gives 34 - 17 = 17, 51 - 34 = 17, 63 - 51 = 12, 99 - 63 = 36, which violates condition 2. Therefore, these ducks do not satisfy the Law of Equally Spaced Ducks.
Sample 3
Input
10 1299709 1299709 1299709 1299709 1299709 1299709 1299709 1299709 1299709 1299709
Output
NO
This violates condition 1. At this point, it is clear that the Law of Equally Spaced Ducks is not satisfied. Note that coordinates can be large values.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
はむ吉🐹
btk