TopCoder problem "UniqueTriangles" used in TCCC '04 Round 2 (Division I Level Two)

Problem Statement


Given n points in the cartesian coordinate system, how many uniquely shaped triangles can you construct by letting 3 of these points be the corners of a triangle? Two triangles are unique if they are not similar - that is, if it's not possible to transform one triangle into the other by any means of rotating, flipping and/or scaling. See example 0 for further clarifications.

Create a class UniqueTriangles which contains the method howMany that takes as input a int[] x and int[] y, the coordinates of the n points, and returns an int, the number of unique triangles. Element i in x and y corresponds to one point.



Parameters:int[], int[]
Method signature:int howMany(int[] x, int[] y)
(be sure your method is public)


-All angles in a triangle must be strictly greater than zero.
-Hint: Two triangles are similar if a1/a2 = b1/b2 = c1/c2 where ai, bi and ci are the sides in one triangle (in some order).


-x will contain between 3 and 50 elements, inclusive.
-y will contain the same number of elements as x.
-Each element in x will be between -10000 and 10000, inclusive.
-Each element in y will be between -10000 and 10000, inclusive.
-No point will occur more than once.


Returns: 3

If we first consider all triangles that can be created using only the points (0,0), (1,1), (1,0) and (0,1), we see that all such triangles will have two sides with length 1 and one side with length sqrt(2).

Now, if we also take into consideration the point (2,0), we also get the triangles:

  a) (2,0)-(0,0)-(0,1)
  b) (2,0)-(0,0)-(1,1)
  c) (2,0)-(0,1)-(1,1)
  d) (2,0)-(1,0)-(0,1)

c) and d) are similar (flip), and b) is similar with the triangles between the first four points (larger and rotated). There are no more possible triangles, so the method should return 3.

Returns: 0
All points lie on a straight line, thus there are no triangles at all. The method should return 0.
Returns: 256
Returns: 9872

Problem url:

Problem stats url:




lbackstrom , schveiguy

Problem categories:

Brute Force, Geometry