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

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

Output:56

**Approach:** The number of ways of choosing **3** points among **4 * N** points is ** ^{(4 * N)}C_{3}**. 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

**for each of the side i.e.**

^{N}C_{3}**4 ***in total. Therefore, the required count of triangles will be

^{N}C_{3}**(**.

^{(4 * N)}C_{3}) – (4 *^{N}C_{3})Below is the implementation of the above approach:

## C++

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count ` ` ` `// of possible triangles ` ` ` `static` `int` `noOfTriangles(` `int` `n) ` ` ` `{ ` ` ` `int` `y = ` `4` `* n; ` ` ` `return` `((y * (y - ` `2` `) * (y - ` `1` `)) - ` ` ` `(` `4` `* n * (n - ` `2` `) * (n - ` `1` `))) / ` `6` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `1` `; ` ` ` ` ` `System.out.println(noOfTriangles(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of possible triangles ` `def` `noOfTriangles(n): ` ` ` `y ` `=` `4` `*` `n ` ` ` `return` `((y ` `*` `(y ` `-` `2` `) ` `*` `(y ` `-` `1` `)) ` `-` ` ` `(` `4` `*` `n ` `*` `(n ` `-` `2` `) ` `*` `(n ` `-` `1` `)))` `/` `/` `6` ` ` `# Driver code ` `n ` `=` `1` ` ` `print` `(noOfTriangles(n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count ` ` ` `// of possible triangles ` ` ` `static` `int` `noOfTriangles(` `int` `n) ` ` ` `{ ` ` ` `int` `y = 4 * n; ` ` ` `return` `((y * (y - 2) * (y - 1)) - ` ` ` `(4 * n * (n - 2) * (n - 1))) / 6; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `n = 1; ` ` ` ` ` `Console.WriteLine(noOfTriangles(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of triangles with total n points with m collinear
- Number of triangles in a plane if no more than two points are collinear
- Number of triangles formed from a set of points on three lines
- Number of triangles that can be formed with given N points
- Python - Find the maximum number of triangles with given points on three lines
- Forming smallest array with given constraints
- Possible pairs forming a Pythagorean Triple with a given value
- Count of ways to split N into Triplets forming a Triangle
- Count of subarrays forming an Arithmetic Progression (AP)
- Longest subarray forming an Arithmetic Progression (AP)
- Longest subarray forming a Geometic Progression (GP)
- Longest subsequence forming an Arithmetic Progression (AP)
- Count subarrays of atleast size 3 forming a Geometric Progression (GP)
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Minimum number of points to be removed to get remaining points on one side of axis
- Ways to choose three points with distance between the most distant points <= L
- Steps required to visit M points in order on a circular ring of N points
- Find the point on X-axis from given N points having least Sum of Distances from all other points

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.