Given n triangles in cartesian plane, count the number of right triangles

Explanation:

Given a triangle, you have to tell if it is a right triangle. The author and tester used following property of right triangles.

C^{2} = A^{2} + B^{2}

Where C is the length of longest side, A and B are length of other two sides.

int dist_square(point p, point q)
return (p.x-q.x)*(p.x-q.x) + (p.y-q.y)*(p.y-q.y)
bool is_right_triangle(point p, point q, point r)
c = dist_square(p, q)
b = dist_square(p, r)
a = dist_square(q, r)
return 2*max(a, b, c) == a+b+c

The co-ordinates were kept very small so that there are no integer overflow errors.

Other Solution

Editorialistâ€™s solution was based on the fact that dot product of two non zero vectors is zero iff they are orthogonal.

point operator-(point a, point b)
return point(a.x-b.x, a.y-b.y)
int dot(point p, point q)
return p.x * q.x + p.y * q.y
bool is_right_triangle(point p, point q, point r)
point v1 = p-q
point v2 = q-r
point v3 = r-p
return dot(v1, v2) == 0 or dot(v1, v3) == 0 or dot(v2, v3) == 0

donâ€™t use floats, floats yelds precision errors. you should use integer arithmetics to get precise results. So, to remove the floats, you should adapt the last comparisons so you donâ€™t need to divide any variables.

so, for example, s1s2 == -1 would become (y2-y1)(y2-y3) == (x2-x1)*(x2-x3)

for more info on precision errors on using floats, see this: