Data Structures & AlgorithmsHardICPC regional · geometry
Problem: How many collinear triples of points does the 3 x 3 grid {0,1,2}^2 contain? Use the cross-product test below.
Problem: How many collinear triples of points does the 3 x 3 grid {0,1,2}^2 contain? Use the cross-product test below.
from itertools import combinations
pts = [(x, y) for x in range(3) for y in range(3)]
def col(a, b, c):
return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]) == 0
print(sum(1 for t in combinations(pts, 3) if col(*t)))What is printed?- A. 8
- B. 6
- C. 48
- D. 76
Correct answer: A. 8
Collinear triples in the 3 × 3 grid lie on 3 rows, 3 columns and 2 diagonals, so 8 of the C(9,3) = 84 triples have a zero cross product. Forgetting the diagonals gives 6, counting ordered triples gives 8 × 6 = 48, and counting non-collinear triples (triangles) gives 76.
More Coding Interview Problems questions
- Problem: How many integers x in [1, 60] satisfy x ≡ 2 (mod 3) and x ≡ 3 (mod 4)? Run the code. python print(sum(1 for x in range(1, 61) if…
- Problem: How many unordered pairs from {1, 2, 3, 4, 5, 6} have XOR equal to 7? Run the code. python from itertools import combinations…
- Problem: How many palindromic substrings of length at least 1 does 'abcba' contain? Run the code. python s = 'abcba' print(sum(1 for i in…
- Problem: What is the smallest positive integer whose decimal digits multiply to 36? Run the search code. python x = 1 while True: p = 1…
- Problem: How many ways are there to place 2 non-attacking rooks on a 3 x 3 board (distinct squares, different rows and different columns…
- Problem: How many monotone lattice paths from (0,0) to (2,2) exist on a 3 x 3 grid if the moves are right, down or diagonal (down-right)?…