We are given the problem: There are some points on the plane. All but one of them are on the boundary of an axis-aligned square (sides parallel to axes). Identify that point. Input: first line integer n (2 ≤ n ≤ 10). Then 4n + 1 lines each with coordinates (x_i, y_i) with integer coordinates between 0 and 50 inclusive. It's guaranteed that there are at least n points on each side of the square and all 4n+1 points are distinct. Output coordinates of the point not on the boundary.
We need to determine the square's boundaries and then find the point that is not on those boundaries.
Important: The square is axis-aligned. So its sides are lines x = minX (left side), x = maxX (right side), y = minY (bottom), y = maxY (top). But note: The square could have its sides aligned with axes, and its sides are equal in length (since it's a square). So the side length is L = maxX - minX = maxY - minY. However, we don't know which sides are which. The points are given; we need to infer the square.
We know there are at least n points on each side of the square. That is, there are at least n points on each of the four boundary lines (sides). Since total points are 4n+1, and exactly 4n points are on the boundary, the remaining one is interior (or maybe also on boundary but the problem says it's not on the boundary). So there are exactly 4n boundary points: at least n per side.
We need to find the square. One approach: The square's sides must be at coordinates that appear as coordinates of many points. Since all boundary points lie on the sides, they will have either x constant (for vertical sides) or y constant (for horizontal sides). For points on a vertical side, they all share the same x coordinate (the side's x) and have varying y between minY and maxY inclusive (the endpoints are corners? Possibly corners may be counted as part of both sides; but the problem says "on the boundary" so corners are on boundary; they are on two sides. But the count per side includes corners? It says at least n points on each side, so corners are counted for both sides. That is okay.
We need to identify the four lines (x = a, x = b, y = c, y = d) that define the square. Since it's a square, we have b - a = d - c > 0.
Given the points, we can look at the set of x coordinates and y coordinates. The boundary points will have x either equal to minX or maxX or y equal to minY or maxY.
But because there could be interior point(s?) Actually only one interior point, the rest are boundary.
Since there are exactly 4n boundary points, we can try to deduce the square's dimensions.
Observation: For each side, there are at least n points. So the total number of points with x equal to some candidate left side must be at least n, and similarly for right side. But there might be points with the same x coordinate but not on the boundary? They could be interior if the x coordinate is between minX and maxX, but if a point shares the same x as left side, it could only be on that vertical line; but if it's not at y between minY and maxY, it wouldn't be on the boundary? Actually if a point has x = minX, then it is on the left side only if its y is between minY and maxY inclusive. But since all points are distinct and given, and we don't know minY and maxY yet.
However, we can reason: The square's sides are at extreme x and y values among the boundary points? Not necessarily; because the interior point might have coordinates outside the square? No, interior point is inside the square (since it's not on the boundary, but could it be outside? The problem says "All but one of them are on the boundary of an axis-aligned square." So the remaining point is not on the boundary; it could be inside or outside? Typically, "not on the boundary" means either inside or outside? But likely they mean interior point (inside the square) because the points are "on the plane" and "all but one are on the boundary of a square" so the square's boundary contains 4n points; the extra point is not on that boundary. Could it be outside? Possibly yes: if it's outside, then it would not be on the boundary, but then the square boundary only includes those points. However, they say "the point that is not on the boundary of the square." It could be anywhere else. But the problem's examples show interior point (1,1) is inside square from 0 to 2. Also second example: (0,3) is outside? Actually square sides: x=0,2 and y=0,2. So (0,3) has x=0, y=3 which is on the left side extended? The point (0,3) lies on the vertical line x=0, but y=3 is above y=2, so it's outside the square. So it's not on the boundary because boundary only includes y from 0 to 2. So indeed the non-boundary point can be outside the square. In the example, (0,3) is outside but still on the line x=0 but outside the square's vertical side. So it's not on boundary. So the extra point can be anywhere except on the boundary lines segments that form the square.
Thus we cannot simply take min/max of all x or y coordinates to find the square's boundaries because the extra point might extend beyond. In example 2, the points include (0,3) which has y=3 > 2. The other boundary points have y max = 2? Actually boundary points: we have 0,0; 0,1; 0,2; 0,3? Actually second example input:
2
0 0
0 1
0 2
0 3
1 0
1 2
2 0
2 1
2 2
So points: (0,0),(0,1),(0,2),(0,3),(1,0),(1,2),(2,0),(2,1),(2,2). The square sides are x=0, x=2, y=0, y=2. The extra point is (0,3) which is outside (above). So overall min x among all points is 0, max x is 2. min y is 0, max y is 3. So the square's maxY is 2 not 3. So the square's boundaries are not simply the min and max of coordinates across all points. So we need to infer the square from the pattern of points.
Given that there are at least n points on each side, we can try to find four lines (vertical/horizontal) that each contain at least n points, and those lines should define a square (i.e., the vertical lines have constant x values, horizontal lines constant y values, and the difference between x's equals difference between y's). Moreover, all points except possibly one lie on the union of these four line segments (including corners). The extra point will not lie on those segments.
Because n is small (2 to 10) and total points are up to 4*10+1=41, coordinates up to 50, we can brute-force possible squares.
But maybe there is a more deterministic method: The square's sides must have exactly n points? Or at least n. Since total boundary points are exactly 4n, that means that the total number of points on the boundary is exactly 4n (since all but one are on boundary). So the boundary points count = 4n. And we have at least n per side, so the sum of points over sides (counting corners twice) must be 4n. Let a, b, c, d be number of points on left, top, right, bottom sides, counting corners only for that side? If we count points on each side separately, corners are counted twice. Then total counted = a + b + c + d = 4n + (#corners counted extra). Actually if we let A be the set of boundary points; each point on a side contributes 1; corners belong to two sides, so in the sum over sides (count per side) they are counted twice. Thus a + b + c + d = |A| + #corners. Since |A| = 4n, we have a+b+c+d = 4n + #corners. Since a,b,c,d >= n, the minimum sum is 4n (if each exactly n and no corners double counted? But corners cause double counting, so sum would be >4n if there are corners). Actually if each side has exactly n points and there are no corners? That's impossible because the sides meet at corners, and corners are part of both sides. But if a side includes its endpoints, then corners are points on two sides. So to have exactly n points on a side, the corners must be included. For a side of length L (integer coordinates?), the number of integer lattice points on the side (including endpoints) is something. But the problem does not require points to be integer lattice? Actually coordinates are integers, but the square sides could be at integer coordinates? In examples they are. But is it guaranteed? The problem says axis-aligned square, but coordinates of points are integers. It doesn't state that square vertices are integer coordinates. However, since points are on boundary, and they have integer coordinates, the square's sides must pass through integer coordinates? Not necessarily: a square could have side length non-integer, but its boundary might contain integer points; but given constraints small, maybe it's safe to assume the square's boundaries are at integer coordinates? Let's check problem statement: "There are some points on the plane. All but one of them are on the boundary of an axis-aligned square (its sides are parallel to the axes)." It doesn't specify integer vertices. However, the input points have integer coordinates. The square could be defined by any real numbers. But the points that lie on the boundary must satisfy either x = some constant (left or right) or y = some constant (top or bottom). Since the points are integer, those constants must be integers if there is at least one point on that side? Actually if a vertical side is at x = c, and there is a point (c, y0) with integer coordinates, then c must be integer? Not necessarily: the point's x coordinate is integer, so c = that integer, because the point's x coordinate is exactly c. So yes, if there is at least one point on the side, and that point's x coordinate is integer, then the side's x coordinate equals that integer. So each side's coordinate is integer because we have at least n points on each side, all with integer coordinates, so the side's constant coordinate is integer. Therefore the square's boundaries are at integer x and y values. Similarly the side length (difference) must be integer? Since both minX and maxX are integers, side length = maxX - minX is integer. Same for y difference. So square has integer side length.
Thus the square is defined by integers: Xmin, Xmax, Ymin, Ymax, with Xmax - Xmin = Ymax - Ymin > 0.
Now we have up to 41 points, we can attempt to infer Xmin, Xmax, Ymin, Ymax. Since there are at least n points on each side, and total boundary points are 4n, we can hypothesize that exactly n points lie on each side, counting corners only once? But corners are shared, so the total number of distinct boundary points is something. Let's denote the set of points on left side: those with x = Xmin and Ymin <= y <= Ymax. Similarly right: x = Xmax, Ymin <= y <= Ymax. Bottom: y = Ymin, Xmin < x < Xmax? Actually includes endpoints? Typically the bottom side includes points with y = Ymin and Xmin <= x <= Xmax. But corners (Xmin,Ymin) and (Xmax,Ymin) are part of both bottom and left/right. So in counting distinct boundary points, corners appear in two sides but are single points. So total distinct boundary points = (points on left including corners) + (points on right including corners) + (points on top excluding corners? Actually careful: To avoid double counting, we can consider the four sides inclusive of endpoints, then the union count is (left count + right count + bottom count + top count) - 4 corners (since each corner counted twice? Actually each corner appears on exactly two sides, so in the sum they are counted twice. So to get distinct count, subtract the number of corners (4). But if corners are included in each side, then sum = left + right + bottom + top, distinct = left+right+bottom+top - 4. However, bottom and top include the corners as well. So distinct = left+right+bottom+top - 4. Since distinct boundary points count is 4n, we have left+right+bottom+top = 4n+4.
Given each side has at least n points, the sum of points on sides (counting corners multiple times) is at least 4n. To reach exactly 4n+4, the sides must have exactly n+1 points each? Because 4*(n+1) = 4n+4. But wait, maybe the sides can have more than n points? They said at least n points on each side. But total distinct boundary points is exactly 4n. If any side has more than n+1, then the sum would exceed 4n+4, and distinct would exceed 4n, because distinct = sum - 4. Let's denote L = number of points on left side (including both corners), R = on right, B = on bottom, T = on top. Then distinct = L+R+B+T - 4 = 4n. So L+R+B+T = 4n+4. Since each is at least n, the sum is at least 4n. To be exactly 4n+4, the sides must sum to 4n+4. Could we have one side with n+2, another with n, and the rest with n+1? That sum would be n+2 + n + n+1 + n+1 = 4n+4? Actually n+2 + n + n+1 + n+1 = 4n+4. Yes, that works. So the side counts can vary as long as they sum to 4n+4. But we have the guarantee that there are at least n points on each side, and total distinct boundary points are 4n. So the side counts (including corners) must sum to 4n+4. So there are many possibilities. However, we can deduce that the square's boundaries are such that the set of points on them are exactly the given points except one.
Given small n, we can brute-force candidate squares. The square's Xmin, Xmax, Ymin, Ymax are integers between 0 and 50 inclusive. But we can reduce the search space: Since there are at least n points on each side, the side length L = Xmax - Xmin = Ymax - Ymin must be at least n-1? Not exactly: On a vertical side, the number of integer points on that line segment from (Xmin, Ymin) to (Xmin, Ymax) inclusive is (Ymax - Ymin + 1). Since we have at least n points on that side, we need Ymax - Ymin + 1 >= n, so side length L >= n-1. Similarly for horizontal side, L >= n-1. So L is at least n-1, and up to 50 maybe. Since coordinates are 0..50, L can be up to 50. That's a limited range: side length from max(1, n-1) to 50. Actually side length could be 0? No, square must have positive area, so L > 0. So L >= 1.
Now total points: 4n+1 <= 41. So n <=10.
Brute-force over all possible squares: For each possible Xmin from 0 to 50, for each possible Ymin from 0 to 50, for each possible L from 1 to 50 such that Xmin+L <= 50, Ymin+L <= 50 (since coordinates of points are up to 50, but the square boundaries could be outside that range? Actually points are within 0..50, and all boundary points are within that range, so the square boundaries must be within that range because boundary points lie on the sides and corners must be within 0..50. So Xmin, Xmax, Ymin, Ymax all between 0 and 50. So we can restrict to 0..50). So there are at most 515151 = 132651 possibilities, which is fine. For each square, we can check: Count how many of the given points lie exactly on the boundary (i.e., either x = Xmin with Ymin <= y <= Ymax, or x = Xmax with Ymin <= y <= Ymax, or y = Ymin with Xmin <= x <= Xmax, or y = Ymax with Xmin <= x <= Xmax). Also note that points on corners satisfy two conditions but should be counted only once. We need that the number of points on boundary is exactly 4n, and that exactly one point is off the boundary (i.e., total points - boundary count = 1). Moreover, we need to ensure that there are at least n points on each side. But if boundary count is exactly 4n and there are at least n per side, automatically? Not necessarily; it's possible that boundary count is 4n but one side has less than n, compensated by extra on other sides? But total distinct boundary points is 4n, and sides counts (including corners) sum to 4n+4. If one side has less than n, then sum would be at most (n-1)+? But let's just check that for each side the count of points (including corners) is at least n. That is easy to compute.
Also we need to ensure that the square is indeed axis-aligned and that the points on the boundary are exactly those 4n points; the extra point is the one we output. Since the problem guarantees exactly one such square? It should be unique due to the conditions. So we can iterate, find the square for which the conditions hold.
But we must also handle the case where there are multiple squares satisfying? Given problem statement and guarantee of uniqueness, we can find the one that matches.
Implementation steps:
- Read n.
- Read list of points (size m = 4n+1). Store in list.
- For each candidate square: X from 0 to 50, Y from 0 to 50, L from 1 to 50 such that X+L <= 50, Y+L <= 50.
- Compute Xmin = X, Xmax = X+L, Ymin = Y, Ymax = Y+L.
- Count boundary points: For each point p = (x,y), if (x == Xmin and Ymin <= y <= Ymax) or (x == Xmax and Ymin <= y <= Ymax) or (y == Ymin and Xmin <= x <= Xmax) or (y == Ymax and Xmin <= x <= Xmax) -> boundary.
- If number of boundary points == 4n, then we also need to check that number of points NOT on boundary == 1 (automatically since total points = 4n+1). But we also need to check that each side has at least n points. Let's compute:
left_count = count of points with x == Xmin and Ymin <= y <= Ymax.
right_count = count with x == Xmax and Ymin <= y <= Ymax.
bottom_count = count with y == Ymin and Xmin <= x <= Xmax.
top_count = count with y == Ymax and Xmin <= x <= Xmax.
- If left_count >= n and right_count >= n and bottom_count >= n and top_count >= n, then this square is valid. Since boundary count exactly 4n, and per side counts sum to 4n+4 (including corners), it should be consistent.
- Note: It's possible that multiple squares might satisfy? But we can break after finding first valid square because problem guarantee.
- For that valid square, find the point that is not on boundary (i.e., the one point where the condition failed). Print its coordinates.
We must consider that the square's boundaries could be at the extremes of coordinates (0 or 50). That's fine.
Time complexity: around 515151 = 132,651, times up to 41 points per check => about 5.4 million ops, trivial.
Edge cases: n small, e.g., n=2, side length at least 1? For n=2, side length L must be such that there are at least 2 points on each side. On a vertical side, number of integer points on line segment inclusive is L+1 (since Ymax - Ymin = L). So we need L+1 >= 2 => L>=1. So L can be 1. Check if that's possible: If L=1, then square is a unit square of side 1. Then each side has exactly 2 endpoints (corners) which are the two corners of that side. But that gives exactly 2 points on each side? For example, left side from y=0 to y=1 inclusive includes points (0,0) and (0,1) -> 2 points. So that matches at least n=2. But total distinct boundary points: For a unit square, the set of boundary points includes the 4 corners. But that's only 4 points, not 4n=8. So it's impossible because we need 4n boundary points distinct. So L must be larger to accommodate 8 distinct boundary points. Indeed, for n=2, we need 8 distinct boundary points. With L=1, max distinct boundary points = 4 (corners) + (L-1)? Actually for integer coordinates, the boundary of an axis-aligned square with integer corners includes all integer points on its perimeter. If side length L, the number of integer lattice points on the perimeter (including corners) is 4L (if L>0). Actually count: For a square with corners (x0,y0), (x0+L, y0), (x0+L, y0+L), (x0, y0+L). The number of integer points on the boundary:
- Bottom side: points from (x0, y0) to (x0+L, y0): L+1 points.
- Top side: (x0, y0+L) to (x0+L, y0+L): L+1 points.
- Left side: (x0, y0) to (x0, y0+L): L+1 points, but corners (x0,y0) and (x0,y0+L) already counted in bottom and top. So distinct boundary count = 2*(L+1) + 2*(L-1) = 4L. Let's compute: bottom: L+1, top: L+1, left excluding corners: L-1 (since endpoints already counted), right excluding corners: L-1. So total distinct = (L+1)+(L+1)+(L-1)+(L-1) = 4L. Yes. So distinct boundary points = 4L. In our problem, we need exactly 4n boundary points. So 4L = 4n => L = n. So the side length must be exactly n? Let's check: Example 1: n=2, square from 0 to 2 => L=2, and 4L=8=4n. Indeed. Example 2: n=2, L=2. So it appears that due to the constraints "at least n points on each side" and total distinct boundary points exactly 4n, the side length must be n, and the boundary must contain exactly all integer points on the perimeter of the square. Because the number of integer points on the perimeter of an axis-aligned square with integer coordinates is 4L (since L is side length). And we have exactly 4n points on boundary. So we must have 4L = 4n, so L = n. Also each side must have exactly L+1 points (including corners) or maybe less? Wait, the number of integer points on a side of length L (including both endpoints) is L+1. For the left side, we have L+1 points. But we only need at least n points. Since L = n, L+1 = n+1, so each side actually has n+1 integer points (including corners). That would sum sides counts (including corners) to 4*(n+1) = 4n+4, which matches our earlier equation. However, the distinct boundary count is 4L = 4n. So indeed, the boundary points consist of all integer points on the perimeter of the square. But is that forced? Let's verify: Could there be some integer points missing on the boundary? The problem only says "All but one of them are on the boundary". It does not require that all integer boundary points are present. It says there are at least n points on each side, but total boundary points is 4n. Since the maximum number of integer points on the perimeter is 4L, where L is side length. If L is larger than n, then 4L > 4n, but we only have 4n boundary points, so some integer boundary points might be missing. However, could L be larger than n? Let's consider possibilities.
We need to satisfy: left side count (points actually on left side) >= n. The left side has L+1 possible integer points if endpoints are integers. But we only have some of them present. Similarly for other sides. The total distinct boundary points present is 4n. The sum over sides (including corners) = left+right+top+bottom = (actual counts on each side, including corners) = S. Since each corner is counted twice in S, distinct = S - 4. So S = 4n + 4. So the sum of the counts per side (including corners) is exactly 4n+4. This is independent of L. Now, each side's count cannot exceed the number of integer points on that side: for left side, maximum possible is L+1 (if all integer points are present). So we have left <= L+1, right <= L+1, bottom <= L+1, top <= L+1. Therefore, S = left+right+top+bottom <= 4(L+1) = 4L+4. So 4n+4 <= 4L+4 => n <= L. So L >= n.
Also, each side count >= n. So left >= n, right >= n, top >= n, bottom >= n. So S >= 4n. But we already know S = 4n+4, so that's satisfied. So we have n <= L. Also, the maximum distinct boundary points possible is 4L. Since distinct = 4n, we have 4n <= 4L => n <= L. So L can be greater than n, but then the maximum possible distinct points is 4L > 4n; however we have exactly 4n distinct points on the boundary, which is less than the maximum. Is that allowed? The problem only says "All but one of them are on the boundary of an axis-aligned square." It does not require that the boundary points cover the entire perimeter. They could be a subset. However, there is also the condition that there are at least n points on each side. Could L be > n? Let's try to construct a scenario with n=2, L=3. Then the square from, say, x=0..3, y=0..3. Distinct boundary integer points total 4*3=12 possible. We need exactly 4n=8 boundary points. And we need at least 2 points on each side. Could we pick 8 points on the boundary such that each side has at least 2? Yes, e.g., left side: include (0,0) and (0,3) (two points) – but then we must also ensure right side has 2, bottom 2, top 2. That sums to 8? But careful: corners counted twice. For example, pick all four corners (4 points). Then pick one additional point on each side? Let's try: corners: (0,0),(0,3),(3,0),(3,3) – that's 4. To have at least 2 per side, we need each side to have at least 2 points. Left side currently has corners (0,0) and (0,3) → 2 points, meets requirement. Right side similarly (3,0),(3,3) → 2. Bottom side: (0,0),(3,0) → 2. Top side: (0,3),(3,3) → 2. So already we have 4 points, each side has exactly 2. But we need total boundary points = 8, so we need 4 more points. However, these additional points will increase side counts beyond 2, but that's allowed because the condition is at least n. So we could add points, but then total distinct would become >4. For total 8, we could add four more points, each lying on a side but not corners, e.g., (0,1) on left, (1,0) on bottom, (3,1) on right, (1,3) on top. Then left side count becomes 3 (including corners), etc. Sum of side counts would be: left=3, right=3, bottom=3, top=3 => sum=12. Distinct = 12-4=8. So that works! So L=3 is possible with n=2? But does it satisfy "at least n points on each side"? Yes, each side has 3 >=2. Distinct boundary points = 8 = 4n. So it seems L could be > n.
But we also need to consider the distribution of points: They are given; we don't choose them. The problem input is constructed such that there is exactly one square that satisfies the conditions. Could the square have side length > n? Possibly. The problem doesn't restrict L = n. Let's examine the examples: both have L=n=2. Could there be test cases with L > n? Possibly yes. The constraints n up to 10 and coordinates up to 50, so L could be up to 50. So we cannot assume L=n. However, note that the total number of points is only 4n+1. If L is larger, then there are many integer points on the perimeter (4L). Since we have only 4n boundary points, there will be many missing integer points. That's fine.
Thus brute-force over all squares is safe.
But we can potentially narrow the search: Since there are at least n points on each side, the side length L must be at least n-1? Actually, the maximum number of distinct integer points on a side of length L is L+1. For a side to have at least n points, we need L+1 >= n => L >= n-1. So L could be as low as n-1. But also need total distinct boundary points = 4n. If L = n-1, then maximum distinct boundary points = 4(n-1) = 4n-4, which is less than 4n. So impossible because we need 4n distinct boundary points. Therefore L must be >= n. So L >= n. Good. So L in [n, 50] maybe.
Also, note that the square boundaries must be such that the points on the boundary lie exactly on the sides segments. The extra point is not on the boundary. So brute-force is simple.
Implementation details:
We need to be careful about double counting when checking boundary. We'll just use condition: point lies on boundary if (x == Xmin and Ymin <= y <= Ymax) OR (x == Xmax and Ymin <= y <= Ymax) OR (y == Ymin and Xmin <= x <= Xmax) OR (y == Ymax and Xmin <= x <= Xmax). This includes corners. For counting per side, we need separate counts including corners. That's okay.
We also must ensure the square's side lengths are equal: that is, Xmax - Xmin == Ymax - Ymin. That's inherent in our representation (L).
Now iterate over all possible Xmin, Ymin, L such that Xmin+L <= 50, Ymin+L <= 50. For each, compute counts. If boundary_count == 4n and per side counts >= n, then we have a candidate. Since guarantee unique, we output the off point.
But we need to be careful: There might be squares that also satisfy these conditions but are not the intended one? Possibly the input could be ambiguous, but problem says "It is guaranteed that there are at least n points on each side of the square and all 4n + 1 points are distinct." It does not explicitly say that the square is unique, but given it's a well-defined problem, the square should be uniquely determined. And we can trust that only one square will match.
Thus algorithm is straightforward.
Complexities: 515151 ~ 132k, times 41 ~ 5.4 million, fine.
We'll implement in C++ or Python? The problem could be from Codeforces? Might need to produce code. I'll produce Python solution for readability.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
points = []
for _ in range(4*n+1):
x = int(next(it)); y = int(next(it))
points.append((x,y))
total = 4*n+1
for xmin in range(0, 51):
for ymin in range(0, 51):
# L from n to 50 maybe? Actually L can be from 1 to 50, but we can start from n because L < n would be impossible due to total boundary count? Could still be possible if L < n but we have more than one point per coordinate? But the maximum distinct boundary points is 4L. If L < n, then 4L < 4n, impossible because we have 4n boundary points. So L must be >= n. So we can restrict L from n to 50. But to be safe, we could still loop all L and check; it's fine.
for L in range(1, 51):
xmax = xmin + L
ymax = ymin + L
if xmax > 50 or ymax > 50:
break # for this xmin,ymin, increasing L further will exceed, so break out.
# Actually we need to break when xmax > 50 or ymax >50. So if xmax >50 or ymax>50, break.
# But careful: we are in nested loops, for each L. So we can break out of L loop when condition fails. So:
# if xmax >50 or ymax>50: break
# But we must compute xmax, ymax before break.
# We'll compute and check bounds.
# But I'm thinking to loop L from n to 50? Actually we might do range(n, 51) to reduce but not necessary.
# But we need to break correctly when exceed 50.
# Alternatively, precompute max possible L for given xmin,ymin: maxL = min(50-xmin, 50-ymin). So we can loop L in range(1, maxL+1). That's simpler.
# So let's restructure loops: for xmin in range(51): for ymin in range(51): maxL = min(50-xmin, 50-ymin); for L in range(1, maxL+1): ...
# But we also know L must be at least n, so we can start from max(n,1) but it's fine.
# We'll implement with maxL.
# Let's do properly:
for xmin in range(0, 51):
for ymin in range(0, 51):
maxL = min(50 - xmin, 50 - ymin)
for L in range(1, maxL + 1):
xmax = xmin + L
ymax = ymin + L
# Initialize counters
on_boundary = 0
left = 0
right = 0
bottom = 0
top = 0
for (x, y) in points:
if (x == xmin and ymin <= y <= ymax) or (x == xmax and ymin <= y <= ymax) or (y == ymin and xmin <= x <= xmax) or (y == ymax and xmin <= x <= xmax):
on_boundary += 1
# per side counts (including corners)
if x == xmin and ymin <= y <= ymax:
left += 1
if x == xmax and ymin <= y <= ymax:
right += 1
if y == ymin and xmin <= x <= xmax:
bottom += 1
if y == ymax and xmin <= x <= xmax:
top += 1
if on_boundary == 4 * n and left >= n and right >= n and bottom >= n and top >= n:
# Found square. Now find the point not on boundary.
for (x, y) in points:
if not ((x == xmin and ymin <= y <= ymax) or (x == xmax and ymin <= y <= ymax) or (y == ymin and xmin <= x <= xmax) or (y == ymax and xmin <= x <= xmax)):
print(x, y)
return
# If not found (should not happen), maybe fallback.
But note: The per side counts are incremented multiple times if a point is a corner; that's okay because we want counts including corners for each side. The condition left>=n, etc., uses these counts which count corners. That's correct: The problem states "at least n points on each side", and corners are part of each side.
Potential pitfalls: When computing left, we might double count if we use multiple if statements (a corner will satisfy both left and bottom, but we want to count it for each side separately, which is what we do. So fine.
Also, we might count points that are on boundary multiple times for on_boundary? No, we only add 1 for on_boundary because we use a single condition with OR; each point is counted at most once for being on boundary. So on_boundary gives distinct boundary points. That's correct.
Now check with examples.
Example 1: n=2, points: grid 3x3. The square from 0 to 2 (xmin=0, ymin=0, L=2) will have on_boundary: all points except (1,1) -> 8 boundary, left: points with x=0 and y in [0,2] => (0,0),(0,1),(0,2) => 3 >=2, right: (2,0),(2,1),(2,2) => 3, bottom: (0,0),(1,0),(2,0) => 3, top: (0,2),(1,2),(2,2) => 3. So matches. Then the off point is (1,1). Output correct.
Example 2: n=2, points: list. The square (0,0) to (2,2) with L=2 will have boundary points: (0,0),(0,1),(0,2),(1,0),(1,2),(2,0),(2,1),(2,2) => that's 8 points. But we also have (0,3) which is not on boundary. left: points (0,0),(0,1),(0,2) => 3, but note (0,3) is not counted because y=3 > ymax=2. So left=3 >=2, right: (2,0),(2,1),(2,2) =>3, bottom: (0,0),(1,0),(2,0)=>3, top: (0,2),(1,2),(2,2)=>3. So matches. So output (0,3). Good.
Thus algorithm works.
But is there any nuance? The square might not have corners at integer coordinates? But we argued they must be integers because points on sides have integer coordinates and at least one point per side implies the side's constant coordinate is integer. However, consider the possibility that a side might have points but none of them are exactly at the corner? But we have at least n points on each side, and those points are on the side segment. The side is defined by a line. The side segment is between the two corners. The corners themselves may not be among the given points. Could the square's corner coordinates be non-integer? For example, square from x=0.5 to 2.5, y=0.5 to 2.5. Points on the left side would have x=0.5, but x coordinate must be integer because points have integer x. So a point with x=0.5 is impossible. So for a side to contain any integer-coordinate point, the side's x (or y) must be integer. Since each side has at least n >=2 points, the side's constant coordinate must be integer. So corners have integer x and y? For a square, if xmin and xmax are integers, and ymin and ymax are integers, then corners are integer. So square's vertices are integer. So our integer search is valid.
But could the square's side length be not integer? If xmin and xmax are integers, then side length = integer. So fine.
Thus we can restrict to integer boundaries.
Now, we need to consider that there might be multiple squares that satisfy on_boundary == 4n and side counts >= n. Is it possible? Possibly if the extra point lies exactly on the boundary of a larger square? But then it would be counted as boundary, changing counts. For the true square, the extra point is not on its boundary. For a different square, if the extra point happens to be on its boundary, then that square would have boundary count = 4n+1 (since all points would be boundary), which fails the condition on_boundary == 4n. So that's not a problem. But maybe a different square could also have exactly 4n boundary points because some other point that is on the true boundary might be interior for that square? For a different square, the set of boundary points would be different. Could it also satisfy that exactly one point is off boundary? Possibly if the extra point is off, and some other point that is on true boundary is also off for the false square, then boundary count would be 4n-? Actually total points 4n+1. For a false square to have exactly 4n boundary points, it would need that exactly one point is off its boundary. But that point would be some point. It might coincide with the true extra point or another. So there could be two different squares each having exactly one point off boundary. But the problem likely ensures uniqueness. However, to be safe, we might want to ensure that we pick the correct square by also checking that the side counts are at least n. That might still allow multiple squares? Let's test some possibilities.
Consider n=2, points from example1. Could there be another square that meets conditions? For instance, square from xmin=-1? But we restrict to 0..50, and points have coordinates >=0, so min x among points is 0. Could a square with xmin=0, ymin=0, L=3? Then boundary points would be those on that larger square. Which points lie on that boundary? The points with x=0,y in [0,3] include (0,0),(0,1),(0,2) but not (0,3) because not present. (0,3) not present. x=3 not present. y=0 includes (0,0),(1,0),(2,0). y=3 includes none. So boundary count maybe less than 8. Let's compute: Points on boundary of 0-3 square: (0,0),(0,1),(0,2) - left; (1,0),(2,0) - bottom (since y=0, x in [0,3] includes (0,0),(1,0),(2,0) but (0,0) counted already, but we count distinct, so we have left (3), bottom (2 additional? Actually (0,0) already counted, so bottom adds (1,0),(2,0) -> total 5; right side x=3 none; top y=3 none. So boundary count = 5. Not 8. So fails.
What about square from xmin=0, ymin=0, L=1? Then boundary points: (0,0),(0,1),(1,0),(1,1)? Actually square 0-1. Points on boundary: (0,0),(0,1),(1,0),(1,1). But (1,1) is corner? (1,1) is on top-right corner. All four corners present? Actually points: (0,0),(0,1),(1,0),(1,1) are all present? In example1, we have (1,1) present but that's interior for true square? In example1, the set includes all points from (0,0) to (2,2) inclusive. So (1,1) is present. So for square 0-1, boundary points would be (0,0),(0,1),(1,0),(1,1) -> 4. But also other points like (0,2) are not on boundary because y=2 >1. So boundary count=4, not 8. Fails.
So seems unique.
But what about a square offset? For example, consider points: suppose the true square is from 2 to 5 (L=3, n=3 maybe). Could there be another square of same side length but shifted that also includes many points on its boundary? Possibly not if points are arranged specifically.
Given constraints, brute-force will find the correct one.
Now we must consider efficiency in Python: 132k loops * 41 ~ 5.4 million operations, plus some overhead, still fine for Python within time (likely 1 second? Actually Codeforces time limit maybe 1-2 seconds, Python might be okay; 5 million simple operations is fine. But we also have loops over coordinates 0..50 inclusive, so 5151=2601 combos of (xmin, ymin). For each, maxL up to 50, but average maybe 25. So total squares ~260125 ~ 65,025. That's even less than 132k. Actually careful: 0..50 inclusive gives 51 values each, so xmin 51, ymin 51 = 2601 pairs. For each, maxL = min(50-xmin, 50-ymin) which on average is about 25. So total squares about 2601 * 25 = 65,025. That's even better. So we'll have at most ~65k squares. For each we iterate over up to 41 points => ~2.66 million operations. Very fast.
Implementation details: We'll read input and compute.
One nuance: The square must be axis-aligned and sides parallel to axes. Our representation ensures that.
Edge Cases: The extra point could be exactly on the line extension of a side but outside the segment. That point will not be counted as boundary. Good.
Now consider if the square can be degenerate? No, L>=1.
Now we should also consider the possibility that multiple squares satisfy the condition. The problem statement likely expects a unique answer. But just in case, we might want to ensure we pick the one that matches the problem's guarantee. If multiple, we could output any? But probably not. I'd trust.
Potential bug: The condition for being on boundary includes the corners. That's fine.
Another thing: When counting per side, we count a point multiple times if it belongs to two sides (corner). That's correct for checking per side counts.
But there is a subtlety: The condition "at least n points on each side" might count corners only once per side? That's what we did; a corner is part of each side, so it's included in each side's count. So we count it for both sides. That's correct.
Now test with a more complex case: n=3, suppose square from 1 to 4 (L=3). Boundary points are all integer points on perimeter: left: (1,1),(1,2),(1,3),(1,4) -> 4 points; right: (4,1),(4,2),(4,3),(4,4) ->4; bottom: (2,1),(3,1) (excluding corners) ->2; top: (2,4),(3,4) ->2; total distinct = 4+4+2+2=12=4n? For n=3, 4n=12, L=3, 4L=12. So that works. Suppose we have all these 12 points plus an extra point inside. Then per side counts: left=4 (>=3), right=4, bottom=2? Actually bottom count (including corners) would be (1,1),(4,1) plus the points (2,1),(3,1) => total 4? Wait bottom side: points with y=1 and x from 1 to 4 inclusive: (1,1),(2,1),(3,1),(4,1) => 4 points. So bottom count is 4, which is >=3. So okay. So the condition holds. In our counting method, we count bottom: points with y==ymin and xmin<=x<=xmax. That includes corners (1,1) and (4,1). So bottom count = 4. Good.
Now suppose we are missing some points, e.g., we have exactly 12 boundary points but some missing from bottom side. Could that happen? For example, if we miss (2,1) and (3,1) from bottom, then bottom side would have only the two corners (1,1) and (4,1) => count=2, which is less than n=3, fails condition. So the square wouldn't be valid. So the square must have at least n points on each side, so bottom count must be >=3. Since corners are always present? Are corners guaranteed to be among the points? The condition says at least n points on each side. For a side, corners are part of that side. But does it require that the corners are present? Not explicitly. It says there are at least n points on each side. The side includes its endpoints. So if corners are missing, we could still have at least n points on the side if L is large enough and we have many interior points. For example, consider side length L=5 (so possible points on side: 6). If we have 3 points on that side (n=3), we could have points at y = 1,2,3 but not at y=0 or 5. Then left side count would be 3, which is >= n. But corners missing. Is that allowed? The side is the line segment from (xmin, ymin) to (xmin, ymax). The points on the side are those points with x=xmin and ymin <= y <= ymax. The corners are at ymin and ymax. If those are not among the given points, it's still a valid side; the square boundary is still defined by those lines, but the corners may not be occupied by points. However, the square still has those corners (geometric points) but we don't have input points there. So it's possible that some corners are missing. In that case, the square's boundaries are still defined by those extremes. Our brute-force would still detect the square if the boundary counts match and per side counts >= n, even if corners are missing.
But is that scenario consistent with total distinct boundary points = 4n? Let's explore. Suppose square L=5, n=3. Maximum distinct boundary points = 4L = 20. We need only 12. So we can pick 12 points on the boundary. For each side, we need at least 3 points. It's possible to have left side with points at y=1,2,3 (3 points) but not at y=0 or 5. So left count = 3, includes no corners. Similarly right side with points at y=1,2,3 (3 points). Bottom side: need at least 3 points; if we use points at x=1,2,3 but not corners, that's 3 points. However, bottom side includes points with y=ymin, and if we use points at x=1,2,3, we have (1,ymin), (2,ymin), (3,ymin). But note that (1,ymin) might also be considered a corner if xmin=0? Let's define square: xmin=0, xmax=5, ymin=0, ymax=5. If left side missing corners, left points at y=1,2,3. Right side at y=1,2,3. Bottom side: points at x=1,2,3 (y=0). That gives bottom points (1,0),(2,0),(3,0). Those are not corners because corners are (0,0) and (5,0). So bottom count =3. Top side: points at x=1,2,3 (y=5). That's 3. Now total distinct boundary points: left 3, right 3, bottom 3, top 3 = 12. But note: Are there any overlaps? Left and bottom share? Left points have x=0, bottom points have y=0, they could overlap at (0,0) which is not included. So no overlap. Right and bottom could overlap at (5,0) not included. Similarly corners absent. So distinct count = 12. So such a configuration is possible. So the square could have missing corners. Then the square's boundaries are still determined by the extremes: xmin=0, xmax=5, ymin=0, ymax=5, even though there are no points at those corners. So our brute-force approach would still identify this square because the points that are on the boundary satisfy the segment conditions. But would the side counts be computed correctly? Yes: left count would be 3 (points with x=0 and 0<=y<=5) which is 3, meets n. So it's valid.
Now, could there be another square that also satisfies these conditions? Possibly a smaller square inside? Let's test. Suppose points as described: square (0,0)-(5,5) with missing corners, left points at y=1,2,3; right at y=1,2,3; bottom at x=1,2,3; top at x=1,2,3. Could there be a square (1,1)-(3,3) with L=2? Then boundary: left x=1 with y in [1,3]: points (1,1),(1,2),(1,3) are present? (1,1) maybe? In our set, (1,1) is not listed? Actually we have bottom points (1,0), (2,0), (3,0) and top points (1,5),(2,5),(3,5), left points (0,1),(0,2),(0,3), right points (5,1),(5,2),(5,3). So (1,1) is not present. So (1,1) not in set. For square (1,1)-(3,3), boundary points would need points on its perimeter: left x=1 y=1,2,3; but we don't have (1,1) or (1,2)? Actually we have left points (0,1),(0,2),(0,3) but not (1,1). So square (1,1)-(3,3) would have few boundary points, not meeting counts.
What about a larger square? Not likely.
But we must ensure that the condition of exactly 4n boundary points is tied to the true square. So if there exists another square with 4n boundary points and per side counts >= n, it might cause ambiguity. However, given the constraints, the problem likely ensures uniqueness.
Now, can we have squares where the extra point lies on the boundary of another square but not on the true one, making that other square also have exactly one point off boundary? For that to happen, the set of points would need to have exactly one point off the boundary of that other square. That would mean that all other points lie on its boundary. So that other square would have to be such that its boundary passes through almost all points. Could that happen? Possibly if points are arranged in a grid? Let's test with example 1: points are full 3x3 grid. Could there be another square? For example, square from (0,0) to (1,1) includes points (0,0),(0,1),(1,0),(1,1) on boundary. But we have many other points not on that boundary, so fails. Square from (0,1) to (2,3) would have boundaries at y=1,3, x=0,2. Points like (0,2) has x=0, y=2 in range? Yes (0,2) would be on left side (x=0, y between 1 and 3). So many points would be on boundary? Actually let's check: Points in example1: (0,0) to (2,2). Square (0,1)-(2,3): boundary: x=0, y∈[1,3] includes (0,1),(0,2) but not (0,0) because y=0<1. So (0,0) is not on boundary. x=2 includes (2,1),(2,2). y=1 includes (0,1),(1,1),(2,1). y=3 includes none. So boundary points: (0,1),(0,2),(2,1),(2,2),(1,1). That's 5 points, not 8. So fails.
Thus brute-force should be safe.
Now, we need to implement efficiently.
Potential optimization: Since we know the square side length L must be >= n, we can start L from n to maxL. This reduces iterations but not necessary. However, we must be careful: Could L be exactly n but still missing corners? That would imply side length = n, so total possible distinct points on perimeter = 4n. If we have exactly 4n boundary points, and we have at least n points per side, then we must have all perimeter points present? Let's analyze: For L = n, maximum distinct boundary points = 4n. If we have exactly 4n boundary points, then it means every integer point on the perimeter is present. Because the count of boundary points present cannot exceed 4n. So if it's exactly 4n, then it must be the full set. In that case, each side would have L+1 = n+1 points (including corners). So per side counts would be n+1, which is >= n. So it's possible. If L > n, then we have less than full perimeter; the boundary points are a subset of size 4n out of 4L possible. So corners might be missing. So L could be greater than n.
Thus we cannot restrict L to n only; we must consider L > n as well. But is L > n possible given the constraints? Let's check if there exists a scenario with L > n and still exactly 4n boundary points. Yes, as constructed earlier. However, is it possible under the condition that there are at least n points on each side? Yes. For L > n, the maximum points per side is L+1, which is > n, so we can choose n points on each side, possibly missing corners. The total distinct points would be at most 4n if we avoid overlaps? But note that if we avoid corners, the sides are disjoint sets (since corners are the only points that belong to two sides). So if we select points on each side that are not corners, then the distinct counts from each side are simply the counts. For each side we need at least n points, we could take exactly n points on each side, all not corners, giving total distinct = 4n. That's possible if each side has at least n interior points (i.e., L+1 - 2 (excluding corners) >= n, i.e., L-1 >= n => L >= n+1). So if L >= n+1, we can pick exactly n points on each side from the interior of that side (excluding endpoints) and get total distinct = 4n. So L > n is feasible.
But wait, the sides are line segments. The interior of a side (excluding endpoints) has L-1 integer points (if L is integer). For L >= n+1, L-1 >= n, so we can pick n interior points. So yes.
Thus L can be n+1 or larger. However, can L be arbitrarily large up to 50? Possibly, but then the number of points per side (n) is small relative to L. So the brute-force must cover L up to 50.
Now, note that total points is 4n+1 <= 41. So 4n <= 40, n <= 10. So if L is large, say 50, then the boundary points are sparse on the perimeter. That's okay.
Now, are there any constraints that the square must be such that its sides pass through the given points? Yes, that's the definition. So brute-force is solid.
Potential pitfalls: Overcounting per side when a point lies exactly on a corner: In the condition for on_boundary we used OR, so it's counted once. For per side counts we used separate if statements, which will each increment for a corner. That's correct.
Now, we need to handle input reading properly.
One more nuance: The input format shows blank lines? In problem statement, there is a blank line before examples? Actually the example input shows:
2
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
There is no extra blank line. So we can read as described.
Now, I'll produce final solution in Python.
But we should be careful about the loops: For each square, we compute counts by iterating over points. That's okay.
Alternative smarter method: Because n is small, we can also deduce the square by considering the frequencies of x and y. For example, for each x coordinate, count how many points have that x. The vertical sides will have many points with that x, and those points will have y values that span a range. But the brute-force is simpler.
Now, test with some custom cases.
Case 1: n=2, points as in example1 -> we already tested.
Case 2: n=2, L=3 scenario. Construct points: square from 0 to 3 (L=3). Choose n=2 points on each side, but total distinct must be 8. Let's construct: left side: pick y=1,2 (2 points). Right side: y=1,2 (2 points). Bottom side: x=1,2 (2 points). Top side: x=1,2 (2 points). That yields points: (0,1),(0,2),(3,1),(3,2),(1,0),(2,0),(1,3),(2,3). That's 8 distinct. Plus one extra point, say (5,5) (any not on boundary). Then input: n=2, total points = 9. Let's see if our algorithm finds the square (0,0)-(3,3). Check: For square (0,0)-(3,3), left count = points with x=0 and y between 0 and 3: we have (0,1),(0,2) => 2, which is >=2. Right: (3,1),(3,2) => 2. Bottom: (1,0),(2,0) => 2. Top: (1,3),(2,3) => 2. Boundary distinct = 8. Extra point (5,5) not on boundary. So it works. Could there be another square, say (1,1)-(4,4) with L=3? Then boundary: left x=1, y∈[1,4] includes? We have points (1,0) but y=0 not in [1,4]; (1,3) is present (since (1,3) is top side point of original square) Actually (1,3) has y=3, x=1, so it would be on left side of square (1,1)-(4,4) because x=1, y between 1 and 4 -> yes. Right side x=4: we have (4,?) not present. Bottom y=1: we have (0,1) but x=0 not between 1 and 4; (1,1) not present; (2,1) not present; (3,1) present? (3,1) is right side point of original, but (3,1) has x=3, y=1, would be on bottom side of square (1,1)-(4,4) if y=1 and x between 1 and 4 -> yes. Top y=4: we have (1,4)? Not present. So boundary count for this square would be maybe (1,3),(3,1) plus perhaps others? (2,?) etc. Not enough. So fails.
Thus brute-force works.
Now, note that we might have to consider squares where the extra point lies exactly on a side line but outside the segment. That is fine because it's not counted as boundary if it's outside the segment.
One more nuance: The boundary condition should be inclusive of corners. We did that.
Now, implement solve().
But we can possibly speed up by precomputing something. Not needed.
Implementation details in Python:
We'll read all ints: data = sys.stdin.read().split(); then n = int(data[0]); then points = list of tuples.
Then we iterate over candidate squares.
Because we need to find the point not on boundary, we can after finding square, iterate through points to find the one that fails the boundary condition. Or we could have kept track while counting: we could store which points are on boundary for that square, but that would require storing a boolean array per square? Better to just recompute after finding square. Since we have the square parameters, we can loop through points again.
Potential performance: after finding square, we break and print.
Now, ensure we break out of loops after finding.
Edge case: If no square found, maybe we output something? But problem guarantees existence.
Now, there is a subtlety: In the loops, we compute per side counts. But we must ensure that we count points for each side correctly even if they are counted multiple times for different sides; that's fine.
One more nuance: The condition "at least n points on each side" means that the side must have that many points from the given set that lie on that side segment. Our counting includes all points that satisfy x == xmin and y between ymin and ymax inclusive. That's correct.
Now, we need to guarantee that the square we find is the correct one. I'll trust the problem.
Now, we can implement.
Testing with examples:
Example1:
n=2, points list.
We loop:
xmin=0, ymin=0, maxL = min(50,50)=50, L from 1 to 50. For L=2, we get counts as before and will match. It will print (1,1). All good.
Example2: similar.
Now, check if any other square might coincidentally also satisfy: For L=2, xmin=0, ymin=0 is the only one. For L=3 with same xmin=0,ymin=0, we computed boundary count=5, not 8. So only one.
Now, potential pitfalls: The square may have xmin > 0 and ymin > 0. Our loops cover that.
Now, consider if the square is at the edge of coordinate range (like xmin=0, xmax=50). That's fine.
Now, we need to ensure that we do not break prematurely: In our loops, we used for L in range(1, maxL+1). So that includes L up to maxL.
Now, we should consider maybe the square could be defined with xmin > xmax? No.
Now, done.
But wait: There is a nuance: The input might have points that are not necessarily forming a "full" perimeter. Our algorithm will still find the square.
Now, think about degenerate case: The extra point might be exactly on a side line but outside the segment, which we treat as not boundary. Good.
Now, could the square be such that the extra point lies exactly on one of the lines x = xmin or x = xmax but with y outside [ymin, ymax]? That's not boundary, so fine.
Now, what about the condition "at least n points on each side"? This includes the possibility that some side might have more than n points. In our check we require >= n.
Now, one more check: In our per side counts, we count points that satisfy the condition (e.g., x == xmin and y between ymin and ymax). That includes points that also satisfy another condition (corner), but we still count them for each side. That's correct.
Now, let's implement.
But careful: When we compute left, right, bottom, top counts, we increment for each condition separately. For a corner point, it will be counted in both left and bottom, etc. That's correct because per side we want to include corners.
Now, but there is a subtlety: If a point lies exactly at a corner, our condition for on_boundary will be true because of the OR, and we count it once. For side counts, we will increment left and bottom (or top/right). That's fine.
Now, potential bug: For points that lie exactly on the boundary but also on the line of two sides (corner), they are counted in both side counts. That's okay.
Now, check if the condition on_boundary == 4n might be influenced by double counting? We are counting distinct because we used a single OR, so each point counted at most once. So fine.
Now, we should also verify that if a point lies exactly on the boundary but is counted multiple times in side counts, that's fine.
Now, I'll write code.
Potential micro-optimizations: Pre-store points in list. Use local variables for speed.
We can also early break out of L loop if boundary count exceeds 4n? Not needed.
Now, finalize.
But we also need to consider that the square might be determined by the min and max x among points that appear many times? Actually we can reduce search space by noting that the square's sides must be lines where many points align. But not necessary.
One more nuance: Could there be squares where the per side counts are >= n but some of those points are actually not on the side because they have the same x but y outside the range? Our condition includes the range check, so fine.
Now, we must be careful with inclusive boundaries: For side counts, we include points with y exactly ymin and ymax. That's correct.
Now, think about scenario where the extra point lies inside the square. That's fine.
Now, test with a more complex scenario: Let's create a test: n=3, L=4 (square from 1 to 5). Choose 3 points on each side: left: (1,2),(1,3),(1,4) (but note (1,2) has x=1,y=2). Right: (5,2),(5,3),(5,4). Bottom: (2,1),(3,1),(4,1). Top: (2,5),(3,5),(4,5). That's 12 points. Extra point: (0,0). Now, does our algorithm find square (1,1)-(5,5)? Let's check: For that square, left count = 3 (points (1,2),(1,3),(1,4) all with y between 1 and 5) -> 3 >= 3; right=3; bottom=3; top=3; boundary distinct = 12. So valid. For square (0,0)-(4,4) maybe? Check: left x=0 has no points; bottom y=0 has only (0,0) but that's extra point; not enough. So not valid.
Thus algorithm works.
Now, implement.
One more check: If the square's side length L is exactly n, then the per side counts are n+1 if all points present. Our condition left>=n is satisfied. Good.
Now, handle input: We'll use sys.stdin.read().
Now, we produce final answer.
But the problem says "The first line contains an integer n (2 ≤ n ≤ 10). Each of the following 4n + 1 lines contains two integers x_i, y_i (0 ≤ x_i, y_i ≤ 50), describing the coordinates of the next point." So the total number of lines after first is 4n+1. So we must read exactly that many.
Now, implement solve().
Now, can we make it faster? Probably not needed.
Now, about the output: Just two integers separated by space.
Now, final code.
But before finalizing, think about potential pitfalls: The square might not have its corners within the coordinate range 0..50 if the square extends beyond? But all points are within 0..50, and the boundary points must be among the points. Since points are within 0..50, the square's corners (if present) would be within 0..50. If corners are not present as points, the square could theoretically have corners outside 0..50? Could that happen? Suppose all boundary points have x in range [0,50] but the square's left side xmin might be less than 0? For a point on the left side, its x coordinate equals xmin. Since points have x >= 0, xmin must be >= 0. Similarly, points on right side have x = xmax, so xmax <= 50. So xmin and xmax are within 0..50. Same for y. So square's boundaries are within [0,50]. So our search range is fine.
Now, the square might have xmin = xmax? No, L>0.
Now, perhaps we could use a smarter method: Find candidate xmin and xmax by looking at x-values that appear at least n times (since each vertical side has at least n points). But careful: The vertical side's points all share the same x coordinate, so the x coordinate of that side must appear at least n times among points. However, there could be multiple x-values with count >= n. For example, in a scenario with L>n, the left and right x-values each appear at least n times. Also, there could be interior x-values with many points but those points might not be on a vertical side because they have varying y, but could be on horizontal sides? Actually points on horizontal sides have y constant and x varying, so they don't cause a high frequency of a particular x. So the x-values with high frequency are likely the vertical sides. But there could be coincidences if many points align vertically but are interior? They could be interior if they are not on the boundary; but interior points would have x between xmin and xmax, and y between ymin and ymax, but they would not be on the boundary. So they could have any x. But could there be an interior vertical line with many points? Possibly if the extra point is not on boundary and we have many points on the boundary, but interior points are only the extra one. So there are no other interior points because all but one are on boundary. So any point that is not on the boundary is the extra point. So all other points are on boundary. So for any x that appears multiple times, those points must be on the boundary. So if a x-value appears many times, it could be from a vertical side or from points on horizontal sides that happen to share that x. But points on horizontal sides have varying x, so they could cause some x to appear multiple times, but typically not as many as vertical side (since horizontal sides have L+1 points, but spread across many x's). So the vertical sides will likely have the highest frequencies, but not guaranteed: If L is large and we only have n points on a vertical side, the frequency of that x might be n. Meanwhile, some other x could also appear n times if many points on horizontal sides line up on that x. For example, consider square from 0 to 10 (L=10), n=2. We pick left side points at x=0, y=3 and y=7 (2 points). Right side x=10, y=3,7 (2 points). Bottom side y=0, x=4,6 (2 points). Top side y=10, x=4,6 (2 points). Then x=0 appears 2, x=10 appears 2, x=4 appears 2, x=6 appears 2. So there are multiple x with frequency 2. So not unique. So we cannot rely solely on frequencies.
Thus brute-force is simpler.
Now, one more subtlety: The square's side length must be equal in x and y. In our loops, we enforce that by using the same L. That's correct.
Now, we could also iterate over possible Xmin, Xmax, Ymin, Ymax with Xmax-Xmin == Ymax-Ymin, but our loops are fine.
Now, implement final solution.
Now, check for potential off-by-one: The boundary condition uses inclusive intervals. That's correct because the side includes its endpoints.
Now, check for performance: Loops: 2601 * up to 50? Actually maxL for each (xmin,ymin) is min(50-xmin, 50-ymin), which maximum when xmin=0,ymin=0 is 50, average ~25. So about 65k squares. For each square, we iterate over at most 41 points, do a few comparisons. That's about 2.7 million operations, trivial.
Now, we might also early break out of loops after finding, which is likely early.
Now, implement.
Now, for reading input, we might use sys.stdin.buffer.
Now, final code:
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = 4 * n + 1
points = []
for _ in range(m):
x = int(next(it)); y = int(next(it))
points.append((x, y))
target_boundary = 4 * n
for xmin in range(0, 51):
for ymin in range(0, 51):
maxL = min(50 - xmin, 50 - ymin)
for L in range(1, maxL + 1):
xmax = xmin + L
ymax = ymin + L
on_boundary = 0
left = 0
right = 0
bottom = 0
top = 0
for (x, y) in points:
# Check if on boundary
on = False
if x == xmin and ymin <= y <= ymax:
on = True
left += 1
if x == xmax and ymin <= y <= ymax:
on = True
right += 1
if y == ymin and xmin <= x <= xmax:
on = True
bottom += 1
if y == ymax and xmin <= x <= xmax:
on = True
top += 1
if on:
on_boundary += 1
if (on_boundary == target_boundary and left >= n and right >= n and bottom >= n and top >= n):
# Found the square
for (x, y) in points:
if not ( (x == xmin and ymin <= y <= ymax) or
(x == xmax and ymin <= y <= ymax) or
(y == ymin and xmin <= x <= xmax) or
(y == ymax and xmin <= x <= xmax) ):
print(x, y)
return
if __name__ == "__main__":
solve()
But we can simplify the on check inside the loop: We have duplicated the condition. But it's fine.
We could also break out of loops after finding, but we already return.
Now, test with examples:
Example1: Input as given, output 1 1.
Example2: output 0 3.
Now, test with custom:
n=2, L=3 case constructed earlier:
Input:
2
0 1
0 2
3 1
3 2
1 0
2 0
1 3
2 3
0 0 (extra point)
But note we need 4n+1 = 9 points. We have listed 8 boundary points + extra (0,0). So the list:
0 1
0 2
3 1
3 2
1 0
2 0
1 3
2 3
0 0
Our algorithm should output 0 0? Actually extra point is (0,0), not on boundary? But (0,0) is corner of the square? Square is (0,0)-(3,3). Wait, our square is from xmin=0, ymin=0, L=3. Then (0,0) is a corner, which is on the boundary. But we set (0,0) as extra point, but then it would be on boundary, contradicting that all but one are on boundary. So we cannot have (0,0) as extra if it's a corner. So pick extra point somewhere else, say (5,5). Then points: the 8 boundary points + (5,5). Then algorithm should find square (0,0)-(3,3) and output (5,5). Good.
Let's test quickly with code mentally: For square (0,0)-(3,3), on_boundary count = 8 (all except (5,5)). side counts: left: points (0,1),(0,2) -> 2 >=2; right: (3,1),(3,2) ->2; bottom: (1,0),(2,0) ->2; top: (1,3),(2,3) ->2. So valid. So output (5,5). Works.
Now, check if any other square could also satisfy? For square (0,1)-(3,4) with L=3? That would have xmin=0, ymin=1, xmax=3, ymax=4. Check on_boundary: points: left x=0, y between 1 and 4: (0,1),(0,2) are in range; (0,?) maybe none else -> 2. Right x=3, y between 1 and 4: (3,1),(3,2) ->2. Bottom y=1, x between 0 and 3: (0,1),(3,1) plus maybe (1,1)? Not present -> (0,1) already counted, (3,1) counted -> distinct adds 0? Actually bottom condition: y=1, x in [0,3] includes (0,1) and (3,1) which are already counted as left/right, so distinct remains 2. Top y=4, x in [0,3]: none. So on_boundary distinct = 2? Actually total distinct = (0,1),(0,2),(3,1),(3,2) maybe? That's 4, not 8. So fails.
Thus unique.
Now, think about potential pitfalls: The condition "at least n points on each side" might be satisfied even if some side has more than n points. Our check left>=n etc works.
Now, what about points that lie exactly on the boundary but also are counted multiple times in side counts? That's fine.
Now, consider a scenario where the extra point lies exactly on the line x = xmin but y outside [ymin, ymax]. Then that point is not counted as boundary. However, it would be counted in left count if we didn't have the y range check? But we have y range check in the condition for left count: x == xmin and ymin <= y <= ymax. So for such a point, it would not be counted as left because y outside. Good.
Now, one more nuance: Could there be a square where the extra point is on a side line but outside the segment, and that side line is also used as another side for a different square, causing confusion? Possibly but unlikely.
Now, I think solution is correct.
Now, we must ensure we read all points correctly; the input may have extra whitespace. We used strip and split, which handles.
Now, I'll finalize the answer.
But the problem might be from Codeforces Round #? Possibly we need to handle multiple test cases? The problem statement does not mention multiple test cases. So single case.
Now, final answer: Provide Python code.
But the user may expect explanation? The instruction says "The Cybermen and the Daleks ...", we need to output solution. So we provide code.
Now, we also can give a more efficient method but brute-force is fine.
Potential improvement: Since we are only interested in squares that have exactly 4n boundary points, we could precompute the set of points and check.
Now, final.
Let's write the final solution.
One more nuance: The input n can be up to 10, but 4n+1 lines, but coordinates up to 50. Our loops over 0..50 inclusive are okay.
Now, check for any possibility that the square's boundaries are not within 0..50? We argued they must be because points are within that range and each side has points on it. However, consider the case where the square's left side is at x=0, right side at x=50, bottom at y=0, top at y=50. That's within 0..50 inclusive. So okay.
Now, done.
Now, provide final answer.
But the problem might be from some contest where time limit is tight. Python with 2.6 million operations is fine. We can also break early if on_boundary > target_boundary, but not needed.
We can also compute counts per side more efficiently by using separate loops. But fine.
Now, final code.
I'll produce as final answer.
But wait, there is a potential pitfall: The problem states "All but one of them are on the boundary of an axis-aligned square." It does not explicitly say that the points on the boundary are exactly those 4n points; it says all but one are on the boundary. That implies that exactly 4n points are on the boundary. So our condition on_boundary == 4n is correct. However, could there be points that are on the boundary of the square but not counted because they are duplicates? The points are distinct, so no duplicates.
Now, final answer.
I'll also add a comment for clarity.
Now, produce final solution. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = 4 * n + 1
points = []
for _ in range(m):
x = int(next(it)); y = int(next(it))
points.append((x, y))
target_boundary = 4 * n
# Try every possible axis-aligned square with integer vertices in [0,50]
for xmin in range(51):
for ymin in range(51):
maxL = min(50 - xmin, 50 - ymin)
for L in range(1, maxL + 1):
xmax = xmin + L
ymax = ymin + L
on_boundary = 0
left = right = bottom = top = 0
for x, y in points:
on = False
if x == xmin and ymin <= y <= ymax:
on = True
left += 1
if x == xmax and ymin <= y <= ymax:
on = True
right += 1
if y == ymin and xmin <= x <= xmax:
on = True
bottom += 1
if y == ymax and xmin <= x <= xmax:
on = True
top += 1
if on:
on_boundary += 1
# Check the conditions
if (on_boundary == target_boundary and
left >= n and right >= n and
bottom >= n and top >= n):
# Found the unique square – output the point that is not on its boundary
for x, y in points:
if not ((x == xmin and ymin <= y <= ymax) or
(x == xmax and ymin <= y <= ymax) or
(y == ymin and xmin <= x <= xmax) or
(y == ymax and xmin <= x <= xmax)):
print(x, y)
return
if name == "main":
solve()