Number of triangles that can be formed
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.
Examples:
Input: N = 1
Output: 4
Input: N = 2
Output: 56
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:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of possible triangles int noOfTriangles(int n) { int y = 4 * n; return ((y * (y - 2) * (y - 1)) - (4 * n * (n - 2) * (n - 1))) / 6; } // Driver code int main() { int n = 1; cout << noOfTriangles(n); return 0; } |
4
Recommended Posts:
- Number of triangles that can be formed with given N points
- Number of triangles formed from a set of points on three lines
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Total number of triangles formed when there are H horizontal and V vertical lines
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of triangles after N moves
- Count the number of possible triangles
- Count number of right triangles possible with a given perimeter
- Number of triangles in a plane if no more than two points are collinear
- Number of possible Triangles in a Cartesian coordinate system
- Number of triangles possible with given lengths of sticks which are powers of 2
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Find maximum number that can be formed using digits of a given number
- Largest even number that can be formed by any number of swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



