Circle Pair Relationship Labeler
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Label each pair of circles by their geometric relationship using exact integer squared-distance comparisons and a strict rule order, including degenerate radius-zero cases.
You are given a list of N circle pairs. Each circle is described by three integers [x, y, r]: the center (x, y) and the radius r. Label how the two circles in each pair relate to each other and return the labels in input order.
Let d be the distance between the two centers, and let r1, r2 be the radii.
Labels
Apply the rules below in this exact order. The first rule that matches decides the label.
IDENTICAL: the centers coincide andr1 == r2.CONCENTRIC: the centers coincide andr1 != r2.TOUCHING_EXTERNALLY:d == r1 + r2.TOUCHING_INTERNALLY:d == |r1 - r2|.INTERSECTING:|r1 - r2| < d < r1 + r2.DISJOINT: every other case. This covers circles that are fully apart (d > r1 + r2). It also covers the case where one circle lies strictly inside the other without touching it and the centers differ (0 < d < |r1 - r2|).
Notes
- A radius of
0is allowed and represents a single point. Because of the rule order, a point lying exactly on a circle isTOUCHING_EXTERNALLY. - Equal radii with different centers can never be
TOUCHING_INTERNALLY, because|r1 - r2| = 0whiled > 0. - Coordinates may be negative. If any radius in the input is negative, raise a
ValueError. - Avoid floating point. Compare squared quantities instead:
d²against(r1 + r2)²and(r1 - r2)². All of these are non-negative, so squaring preserves the ordering.
Input / Output
- Input:
pairs, a list ofNitems, each of the form[[x1, y1, r1], [x2, y2, r2]]. - Output: a list of
Nstrings, one label per pair, in the same order.
Constraints
0 <= N <= 2 * 10^5|x|, |y| <= 10^9and0 <= r <= 10^9for valid input- All values are integers
- A negative radius must raise
ValueError - The target is linear time in
N
Examples
Example 1
Input: classify_circle_relations([[[0, 0, 2], [3, 0, 1]], [[0, 0, 1], [0, 0, 1]], [[0, 0, 3], [0, 0, 1]], [[0, 0, 2], [5, 0, 2]]])
Output: ['TOUCHING_EXTERNALLY', 'IDENTICAL', 'CONCENTRIC', 'DISJOINT']
Hints
Hint 1
Never take a square root. Compute dx*dx + dy*dy once and compare it with (r1+r2)^2 and (r1-r2)^2; both are non-negative, so the order is preserved.
Hint 2
Handle the coincident-centers case first, since it decides IDENTICAL versus CONCENTRIC before any distance comparison is meaningful.
Hint 3
Follow the stated rule order exactly: check external tangency before internal tangency, then intersecting, and let everything left over fall into DISJOINT.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat each pair independently. For a pair, compute three integers:
dist_sq = dx² + dy², the squared distance between the centerssum_sq = (r1 + r2)²diff_sq = (r1 - r2)²
Then test the labels in the prescribed order:
- If
dist_sq == 0, the centers coincide. The label isIDENTICALwhenr1 == r2andCONCENTRICotherwise. - If
dist_sq == sum_sq, the label isTOUCHING_EXTERNALLY. - If
dist_sq == diff_sq, the label isTOUCHING_INTERNALLY. - If
diff_sq < dist_sq < sum_sq, the label isINTERSECTING. - Otherwise the label is
DISJOINT. This includes the "one circle strictly inside the other" case.
If either radius is negative, raise ValueError.
Why it is correct
- The distance
d, the sumr1 + r2and the difference|r1 - r2|are all non-negative. For non-negative numbers,a < bholds exactly whena² < b², and the same is true for equality. So comparing squares gives exactly the same answers as comparing real distances, with no rounding error. - The checks run in a fixed order and the first match wins, so each pair gets exactly one label.
- When the centers differ,
dist_sq > 0. If the radii are equal,diff_sq = 0, so the internal-tangency check can never fire. This matches the rule thatTOUCHING_INTERNALLYneeds different radii. - When one radius is 0,
sum_sq == diff_sq. The external check comes first, so a point on a circle isTOUCHING_EXTERNALLY.INTERSECTINGcan never fire for a point, because its strict range is empty. - Any pair not matched by rules 1 to 4 has either
dist_sq > sum_sqor0 < dist_sq < diff_sq. Both are labeledDISJOINTby the fallback.
Complexity
- Time: O(N), with a constant number of integer operations per pair.
- Space: O(N) for the output list, and O(1) extra.
Python integers are arbitrary precision, so squares of values up to about 2·10⁹ cause no overflow. In a fixed-width language, dist_sq can reach about 8·10¹⁸, which still fits in a signed 64-bit integer.
Reference solution
from typing import List
def classify_circle_relations(pairs: List[List[List[int]]]) -> List[str]:
labels = []
for pair in pairs:
(x1, y1, r1), (x2, y2, r2) = pair
if r1 < 0 or r2 < 0:
raise ValueError("radii must be non-negative")
dx = x1 - x2
dy = y1 - y2
dist_sq = dx * dx + dy * dy
sum_sq = (r1 + r2) ** 2
diff_sq = (r1 - r2) ** 2
if dist_sq == 0 and r1 == r2:
label = "IDENTICAL"
elif dist_sq == 0:
label = "CONCENTRIC"
elif dist_sq == sum_sq:
label = "TOUCHING_EXTERNALLY"
elif dist_sq == diff_sq:
label = "TOUCHING_INTERNALLY"
elif diff_sq < dist_sq < sum_sq:
label = "INTERSECTING"
else:
label = "DISJOINT"
labels.append(label)
return labels