We are asked to determine whether a given polygon is convex. The polygon is given as a sequence of points in counter-clockwise order. Constraints: n up to 100, coordinates integer. The polygon is simple (no self-intersections except at endpoints). Definition: convex if all interior angles are ≤ 180° (i.e., it's a convex polygon). Equivalently, for a polygon given in CCW order, it's convex if all consecutive triples of vertices have the same orientation (all cross products of successive edges have the same sign, either all positive (for CCW) or all zero allowed? Actually interior angles ≤180 means it can be non-strictly convex? The definition says all interior angles are less than or equal to 180 degrees. So it includes degenerate cases where some interior angle is exactly 180° (collinear vertices). However, such a polygon is usually considered convex? Typically a convex polygon is defined such that for any two points inside the polygon, the segment is inside; interior angles ≤ 180 is equivalent if the polygon is simple. However, if some vertices are collinear, the polygon may still be convex, but some definitions consider that degenerate convex polygon (still convex). But also if there are collinear points, the polygon might still be considered convex as long as it's not self-intersecting and the region is convex. The problem statement: "all interior angles are less than or equal to 180 degrees." So exactly 180 is allowed. So we need to output 1 if convex, else 0.
We need to check convexity for a simple polygon given in CCW order.
Standard method: For each vertex, compute cross product of vectors (v_i - v_{i-1}) and (v_{i+1} - v_i). For CCW order, all cross products should be >= 0 (non-negative) for convex polygon (since interior angles ≤180). For CW order they would be <=0. But the input is given in CCW order (as per description: "The coordinates of points are given in the order of counter-clockwise visit of them.") So we expect all cross products to be positive or zero. But we must also ensure that the polygon is simple? The constraints say two sides intersect only at common endpoints, so it's simple. So we just check that all cross products have the same sign (or zero) consistently. However, we need to be careful: For a convex polygon, all cross products for consecutive vertices should have the same sign (if vertices are strictly convex) or zero for collinear. Since they gave CCW, we can check if all cross products are >= 0. But is it possible that the polygon is given in CCW but due to collinearity some cross products are zero? Yes, allowed. So condition: For each i, compute cross = (p[i+1] - p[i]) × (p[i+2] - p[i+1]) (using cyclic indices). cross >= 0 for all i. But we must also consider that the polygon might be given in CCW but if it's not convex, some cross products might be negative. However, what if the polygon is convex but orientation is still CCW? It will be positive. So we can just check that all cross products are >=0.
But there is a nuance: Some convex polygons with collinear points could have a cross product of zero. That's okay. However, the condition "all interior angles ≤180" includes collinear vertices as convex. So we should accept those.
But wait: Is a polygon with collinear vertices considered convex? Usually yes, but sometimes they might consider that not a proper polygon (since vertices are not extreme points). However, problem says interior angles ≤180, so zero area angles? Actually interior angle at a collinear vertex is 180 degrees (or could be 0? No, interior angle is 180 if the vertex lies on the straight line between its neighbors, forming a straight line segment; it's still 180. So yes.
Thus algorithm: Iterate i from 0 to n-1, compute vectors v1 = p[(i+1)%n] - p[i], v2 = p[(i+2)%n] - p[(i+1)%n]; cross = v1.x * v2.y - v1.y * v2.x. Check that all cross >= 0? But what about sign? Since orientation is CCW, cross should be non-negative (if the polygon is convex). But we need to also confirm that orientation is indeed CCW. The problem states it is given in CCW order. So we can trust that if the polygon is convex, cross products will be >=0. But if polygon is non-convex, some cross might be negative. However, is it possible that a non-convex polygon still yields all cross >=0? Let's think: For a simple polygon given in CCW order, the cross product at each vertex corresponds to turning left (positive) or right (negative). For a convex polygon, you always turn left (or go straight) as you traverse CCW. For a non-convex (concave) polygon, there will be at least one right turn (negative cross). So condition all cross >=0 is sufficient and necessary for convexity, assuming the polygon is simple and given in CCW order. However, there is a corner case: if the polygon is self-intersecting? But they said two sides intersect only at common endpoints, so it's simple. So we are good.
But we must also consider that the polygon might be given with some vertices collinear but in such a way that the polygon's interior is still convex, but one vertex might cause a cross product of zero, okay.
One more nuance: What if the polygon is not strictly convex but still CCW orientation, and some cross products are zero, but the polygon might have "reflex" elsewhere? The condition all cross >=0 would still detect concave if any negative.
Thus answer: Compute cross for all i; if any cross < 0, output 0 else output 1.
But we need to be careful: For collinear points, cross is zero. However, if we have a degenerate polygon like all points collinear (n≥3). Then cross = 0 for all i. But is that considered a convex polygon? The polygon with collinear vertices? Usually a polygon is defined as a closed chain of line segments. If all points are collinear, the polygon is degenerate (area zero). The problem might consider that not a convex polygon because interior angles? At each vertex, the interior angle could be 180? But if all points are collinear and the polygon is a line segment back and forth? Actually if points are collinear, the polygon would be a line segment with overlapping edges? However, given that the points are distinct and the polygon is formed by connecting them in order and then back to start. If all points are collinear, then the polygon would be a degenerate "flat" shape: essentially a line segment traced back and forth. But would that be simple? For collinear points in order along a line, the polygon would consist of edges along the line, but then the final edge goes from last point back to first. If the points are in order along the line, the polygon might be a simple polygon? Let's think: Suppose points are A, B, C, D along a line in that order. The polygon edges are AB, BC, CD, DA. AB, BC, CD are along the line; DA connects D to A, which is also along the line but going backward across the whole segment. However, if points are collinear and ordered, the polygon will have overlapping edges? For instance, if points are in order A, B, C, D along a line, the polygon edges: AB (from A to B), BC (B to C), CD (C to D), and DA (D to A). The segment DA goes from D to A, which covers the whole segment from D to A, which overlaps with AB, BC, CD? Actually AB, BC, CD are subsegments of the line from A to D; DA is the entire segment from D to A, which includes all interior points. The polygon would be self-intersecting? The sides would share points? The sides AB and DA share endpoint A, BC and DA share interior? Let's analyze: AB is from A to B; DA is from D to A. They share endpoint A, but do they intersect elsewhere? AB and DA are collinear; they overlap along the segment from A to B? Actually AB is part of DA? DA goes from D to A; that includes the segment from B to A (since B lies between A and D). So AB is a subsegment of DA, so they intersect along AB, not just at endpoints. That would violate the condition "Two sides of the polygon can intersect only at a common endpoint." So such a polygon is not allowed per constraints. So collinear points that are not extreme? Actually, if vertices are collinear but not all on the same line? For a convex polygon, if some consecutive points are collinear, they lie on the same edge. That is allowed as long as the polygon edges are just that line segment? In a simple polygon, if three consecutive points are collinear, the middle point lies on the edge between the outer two. That is acceptable, as the side is still a straight line, and the vertex is redundant. However, does that cause non-simple? It might cause overlapping edges? For example, polygon with vertices A, B, C, D where B lies on edge AC? Actually if consecutive collinear, like A, B, C collinear with B between A and C. Then edges AB and BC together form a broken line along the same straight line; but the polygon is still simple because edges AB and BC meet at B, but there's no overlap. However, the polygon interior angle at B is 180 degrees; it's still a simple polygon (though some definitions might consider it degenerate). And the condition "two sides intersect only at a common endpoint" is satisfied because AB and BC share B; AB and other sides? But note that the side AC is not directly present; we have AB and BC. So it's fine. But if we have non-consecutive collinear points, it might cause issues. However, the polygon is simple per constraints; so collinear points can only appear consecutively? Possibly but not required: For a convex polygon, all vertices lie on the convex hull, and collinear vertices can only appear consecutively along edges. So if the polygon is convex and simple, collinear points must be on the same edge. That means they appear consecutively along the boundary. So for a simple convex polygon given in CCW order, collinear points will appear as consecutive points on the same edge; that will produce cross product zero for the middle points. That's fine.
Thus checking cross >= 0 for all i should be sufficient.
But we need to confirm orientation: The input says points are given in counter-clockwise order. However, if the polygon is convex, that orientation should be consistent. If the polygon is non-convex but still given in CCW order (which is possible: you can traverse a concave polygon in CCW order, the orientation of vertices is still CCW overall), the cross products may be sometimes negative? Actually for any simple polygon, if you traverse vertices in order (counter-clockwise), then the interior is always to the left of each edge. For a concave polygon, at a reflex vertex, you turn right (negative cross) while still moving around the polygon? Wait, let's think: For a simple polygon oriented CCW, the interior is on the left of each directed edge. As you go around, you may make left turns (convex vertices) or right turns (reflex vertices). Actually in a CCW polygon, the interior is always left; at a vertex, if the interior angle is less than 180, you turn left (positive cross). If interior angle is greater than 180, you turn right (negative cross). Because the polygon is making a "right turn" to keep the interior on left. For a concave vertex (reflex, interior angle > 180), the turn is right (negative cross). So indeed for a CCW polygon, cross product signs indicate convex (positive) or concave (negative). So if any cross product negative, polygon is concave.
Thus condition: all cross >= 0.
Edge Cases: If all cross = 0, what does that represent? That would imply all consecutive triples are collinear. That would mean all points are collinear. But as argued, a simple polygon cannot have all points collinear unless n=2? Actually can you have a simple polygon with all points collinear and n>=3? Let's try to construct: Suppose we have three points A, B, C on a line in order A, B, C. The polygon edges: AB, BC, CA. The edge CA goes from C back to A, which is collinear with the other points. Does this polygon satisfy the condition that sides intersect only at endpoints? Check: AB and BC intersect at B (endpoint). BC and CA intersect at C (endpoint). CA and AB intersect at A (endpoint). However, do AB and CA intersect at any other point? They are collinear and overlap along segment from A to B? Actually AB is from A to B. CA is from C to A (reverse). These two segments are collinear and they share the endpoint A. But do they also intersect along the segment from A to B? Since B is between A and C? For points A, B, C collinear with B between A and C. Then segment CA goes from C to A, which includes points from C to A. Since B lies between A and C, B is on CA. So AB is a subsegment of CA? Actually CA = C->A includes all points from C to A. The segment AB is from A to B, which is a subsegment of CA (from A to B). So AB and CA overlap along AB, not just at A. This violates the condition that sides intersect only at endpoints. So it's not allowed. However, if points are arranged in a different order along the line? For a closed polygon, the vertices must be distinct and the edges are line segments connecting consecutive vertices. For all points collinear, the polygon would be a "spike"? Let's think of a configuration: vertices not in order along the line: they might be like A, C, B if A<B<C? But the order must be such that the polygon is simple. For a simple polygon on a line, the only possibility is a degenerate polygon that is basically a line segment traced back and forth, but that would cause overlapping edges. Usually a simple polygon cannot have all vertices collinear with n>2 because the closing edge will overlap with other edges. So the problem likely excludes such cases by the condition "Two sides of the polygon can intersect only at a common endpoint." Thus all points collinear is impossible. So we can safely assume cross products will not all be zero; at least some positive if convex.
Nevertheless, our condition (all cross >=0) would output 1 for all cross =0, but that case likely won't occur. If it does, maybe the test includes a polygon that is a line? But per constraints, it wouldn't happen. But to be safe, we might also consider that a polygon with all points collinear is not convex because interior area is zero? However, the definition of convex polygon typically requires non-zero area? Not explicitly; but I think it's safe to assume such test won't appear. Still, we could add a check for collinearity of all points to be safe? The problem says "polygon", which implies a 2D shape; maybe they don't consider degenerate polygons. So we ignore.
Thus algorithm is straightforward.
But we must be careful about indexing modulo n.
Implementation steps:
Read n, then n points as (x_i, y_i). Store in arrays.
For i from 0 to n-1:
p1 = points[i]
p2 = points[(i+1)%n]
p3 = points[(i+2)%n]
v1 = (p2.x - p1.x, p2.y - p1.y)
v2 = (p3.x - p2.x, p3.y - p2.y)
cross = v1.x * v2.y - v1.y * v2.x
if cross < 0: output 0 and return
After loop, output 1.
That's it.
But is there any subtlety? We need to ensure that the polygon is simple, but we trust constraints.
One nuance: The polygon is given in CCW order. However, if the polygon is self-intersecting (but they said not), the cross sign check would not guarantee convexity. But not needed.
Thus answer: print 1 if convex else 0.
Let's test with given examples.
Example 1:
4
0 0
3 1
2 3
0 3
Compute cross products:
i0: p1(0,0), p2(3,1), p3(2,3) => v1(3,1), v2(-1,2) => cross=32 - 1(-1)=6+1=7 >0.
i1: p2(3,1), p3(2,3), p4(0,3): v1(-1,2), v2(-2,0) => cross = (-1)0 - 2(-2)=0+4=4 >0.
i2: p3(2,3), p4(0,3), p0(0,0): v1(-2,0), v2(0,-3) => cross = (-2)(-3) - 00 =6 >0.
i3: p4(0,3), p0(0,0), p1(0,0)? Actually p0 is (0,0), p1 is (0,0)? Wait careful: indices: i3: p4(0,3), p0(0,0), p1(0,0)? But p1 is (0,0) same as p0? Actually points: p0 (0,0), p1 (3,1), p2 (2,3), p3 (0,3). So for i=3: p3 (0,3), p0 (0,0), p1 (3,1). v1 = (0-0,0-3) = (0,-3), v2 = (3-0,1-0) = (3,1) => cross = 0*1 - (-3)*3 = 9 >0. All positive => convex.
Example 2:
5
0 0
2 0
1 1
2 2
0 2
Compute:
p0(0,0), p1(2,0), p2(1,1): v1(2,0), v2(-1,1) => cross=21 - 0(-1)=2>0.
p1(2,0), p2(1,1), p3(2,2): v1(-1,1), v2(1,1) => cross = (-1)1 - 11 = -1-1? Actually cross = (-1)1 - (1)1? Wait formula: v1.xv2.y - v1.yv2.x = (-1)1 - 11 = -1 -1 = -2 <0 => concave => output 0. So algorithm matches.
Thus solution is straightforward.
But wait: There is also the possibility that the polygon is convex but given in clockwise order? The problem says order is counter-clockwise, so we rely on that. But what if the test includes a convex polygon that is given in clockwise order erroneously? That would cause all cross products to be negative, and we would output 0 incorrectly. However, problem statement explicitly says the coordinates are given in order of counter-clockwise visit. So we can assume it's correct.
One more nuance: For polygons with collinear points, cross product zero. That's okay. But we must ensure that the polygon is still convex if zero appears but not all are non-negative? For instance, if some cross >0 and others =0, it's convex. That's fine.
But what about the case where the polygon has a "self-intersecting" but still simple? Not.
Edge case: n=3. Triangle is always convex (provided no collinear degeneracy? Triangle with three distinct points is always convex. Even if collinear? That would be degenerate with area zero, but three points collinear is not a triangle, but still a polygon? Possibly allowed? But if three points collinear, area zero, then it's a degenerate polygon; interior angles: two vertices at endpoints have interior angle 0? Actually if points are collinear: A, B, C on line in order, then polygon edges: AB, BC, CA. The interior? Not defined. Typically such shape is not considered a polygon because it's self-overlapping? As argued earlier, the segment CA overlaps with AB and BC. So not allowed by constraint that sides intersect only at endpoints. So for n=3, points must be non-collinear for it to be simple. So triangle is convex. So for n=3, our algorithm: cross product for each of three vertices? They all will have same sign (positive if CCW). So we output 1.
Thus solution is simple.
But wait: Could there be a scenario where the polygon is convex but some cross product is negative due to orientation being clockwise? Since they guarantee CCW, we ignore.
Thus final answer: implement as described.
Now let's write the code. We'll use Python. Input reading from sys.stdin.
We should also note that we need to compute cross product for each consecutive triple. Using integer arithmetic.
One more nuance: The polygon could be non-convex but still have all cross >=0 if the orientation is clockwise? But we trust CCW. But to be doubly safe, we might check orientation of first non-zero cross and then ensure all cross have that sign (>=0 for CCW) or (<=0 for CW) but since we know orientation, we can just check >=0.
But what if polygon is convex but given in clockwise? Then all cross would be negative, we output 0. That would be incorrect according to problem but maybe test expects 1? But they said order is CCW, so we can assume it's correct.
Hence solution.
Time complexity O(n).
We'll produce Python code.
But I also recall that there is a known trick: For convex polygon check, one can compute signed area via shoelace to confirm orientation, but not needed.
Thus final solution.
Let's produce final answer.
But the user might want explanation. However, the prompt: "For a given polygon g, print '1' if g is a convex polygon, '0' otherwise." So I need to output code? The conversation appears to be a coding problem. The assistant should output the code? Typically in these interactions, the assistant provides a solution. The user says "For a given polygon g, print ..." and gives constraints. The assistant likely should provide code to solve it. So final answer: provide Python solution.
Thus final output: a Python script reading from stdin, computing cross products, printing 1 or 0.
We'll include comments for clarity.
Also note that we need to handle the case when some cross products are zero; they are okay.
I'll write:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
points = []
for _ in range(n):
x = int(next(it)); y = int(next(it))
points.append((x, y))
# assume n>=3
has_negative = False
for i in range(n):
x1, y1 = points[i]
x2, y2 = points[(i+1)%n]
x3, y3 = points[(i+2)%n]
dx1 = x2 - x1
dy1 = y2 - y1
dx2 = x3 - x2
dy2 = y3 - y2
cross = dx1 * dy2 - dy1 * dx2
if cross < 0:
print(0)
return
print(1)
if name == "main":
main()
That's it.
But we could also check for collinearity all zero but not needed.
Thus final answer.
But the instruction: "Print '1' or '0' in a line." So we do that.
One more nuance: Sometimes if the polygon is not strictly convex, we still output 1. That's correct.
Thus solution is ready.
I will output the code with explanation? The user just asked? The conversation didn't specify "write code". But as ChatGPT, we should respond with the solution. Typically in these problem description scenarios, the assistant provides the code.
Thus final answer: Provide code. ```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
points = []
for _ in range(n):
x = int(next(it))
y = int(next(it))
points.append((x, y))
# check all turning directions (cross products)
for i in range(n):
x1, y1 = points[i]
x2, y2 = points[(i + 1) % n]
x3, y3 = points[(i + 2) % n]
dx1 = x2 - x1
dy1 = y2 - y1
dx2 = x3 - x2
dy2 = y3 - y2
cross = dx1 * dy2 - dy1 * dx2
if cross < 0: # right turn -> concave
print(0)
return
print(1) # all turns left or straight
if name == "main":
main()