Forming triangles using points on a square
Given a square with N points on each side of the square and none of these points co-incide with the corners of the square. The task is to calculate the total number of triangles that can be formed using these 4 * N points (N points on each side of the square) as vertices of the triangle.
Input: N = 1
There is one point on each side. So we can make three rectangles by leaving one point and picking other three points out of the four.
Input: N = 2
Approach: The number of ways of choosing 3 points among 4 * N points is (4 * N)C3. However, some of them do not form a triangle. This happens when all the three chosen points are on the same side of the square. The count of these triplets is NC3 for each of the side i.e. 4 * NC3 in total. Therefore, the required count of triangles will be ((4 * N)C3) – (4 * NC3).
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)