A precision-agriculture crew is planning to install a straight-line irrigation boom across a field, and the boom can only pivot correctly if the field's outer boundary is convex, with no inward notches. A drone has already logged the coordinates of every boundary stake, in the exact order they are visited while walking once around the perimeter (either clockwise or counterclockwise).
You are given the n stake coordinates in that walking order. The boundary is convex only if, at every stake, the walker turns the same rotational direction (always toward the left, or always toward the right) before reaching the next stake — never straight through a stake, and never the opposite turning direction at any stake.
n, the number of boundary stakes.n lines: two integers x y, the coordinates of the i-th stake, listed in the order the boundary is walked.Print YES if the boundary traced by the stakes (in the given order, wrapping from the last stake back to the first) is a convex polygon, otherwise print NO.
n stakes have distinct coordinates.Example 1
Input
3 0 0 4 0 0 4
Expected
YES
Explanation
The three stakes form a triangle. Any triangle with non-collinear vertices is convex: walking (0,0) -> (4,0) -> (0,4) and back to (0,0) always turns the same rotational way, so the output is YES.
Example 2
Input
5 0 0 4 0 4 4 2 2 0 4
Expected
NO
Explanation
Walking the five stakes in order, the turn at stake (2,2) bends back toward the field's interior — a reflex notch — which is the opposite rotational direction from the turns at the other stakes. Since the turning direction is not consistent, the boundary is not convex and the output 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 →