# Count of triangles with total n points with m collinear

There are ‘n’ points in a plane, out of which ‘m’ points are co-linear. Find the number of triangles formed by the points as vertices ?

**Examples :**

Input : n = 5, m = 4 Output : 6 Out of five points, four points are collinear, we can make 6 triangles. We can choose any 2 points from 4 collinear points and use the single point as 3rd point. So total count is 4C2 = 6 Input : n = 10, m = 4 Output : 116

Number of triangles =

^{n}C_{3}–^{m}C_{3}

How does this formula work?

Consider the second example above. There are 10 points, out of which 4 collinear. A triangle will be formed by any three of these ten points. Thus forming a triangle amounts to selecting any three of the 10 points. Three points can be selected out of the 10 points in^{n}C_{3}ways.

Number of triangles formed by 10 points when no 3 of them are co-linear =^{10}C_{3}……(i)

Similarly, the number of triangles formed by 4 points when no 3 of them are co-linear =^{4}C_{3}……..(ii)Since triangle formed by these 4 points are not valid, required number of triangles formed =

^{10}C_{3}–^{4}C_{3}= 120 – 4 = 116

## C++

`// CPP program to count number of triangles ` `// with n total points, out of which m are ` `// collinear. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns value of binomial coefficient ` `// Code taken from https://goo.gl/vhy4jp ` `int` `nCk(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `C[k+1]; ` ` ` `memset` `(C, 0, ` `sizeof` `(C)); ` ` ` ` ` `C[0] = 1; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `// Compute next row of pascal triangle ` ` ` `// using the previous row ` ` ` `for` `(` `int` `j = min(i, k); j > 0; j--) ` ` ` `C[j] = C[j] + C[j-1]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `/* function to calculate number of triangle ` ` ` `can be formed */` `int` `counTriangles(` `int` `n,` `int` `m) ` `{ ` ` ` `return` `(nCk(n, 3) - nCk(m, 3)); ` `} ` ` ` `/* driver function*/` `int` `main() ` `{ ` ` ` `int` `n = 5, m = 4; ` ` ` `cout << counTriangles(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program to count number of triangles ` `// with n total points, out of which m are ` `// collinear. ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `// Returns value of binomial coefficient ` `// Code taken from https://goo.gl/vhy4jp ` `static` `int` `nCk(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `[] C=` `new` `int` `[k+` `1` `]; ` ` ` `for` `(` `int` `i=` `0` `;i<=k;i++) ` ` ` `C[i]=` `0` `; ` ` ` ` ` `C[` `0` `] = ` `1` `; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `// Compute next row of pascal triangle ` ` ` `// using the previous row ` ` ` `for` `(` `int` `j = Math.min(i, k); j > ` `0` `; j--) ` ` ` `C[j] = C[j] + C[j-` `1` `]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `/* function to calculate number of triangle ` `can be formed */` `static` `int` `counTriangles(` `int` `n,` `int` `m) ` `{ ` ` ` `return` `(nCk(n, ` `3` `) - nCk(m, ` `3` `)); ` `} ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `5` `, m = ` `4` `; ` ` ` `System.out.println(counTriangles(n, m)); ` ` ` ` ` `} ` `} ` ` ` `//This code is contributed by Gitanjali. ` |

*chevron_right*

*filter_none*

## Python3

`# python program to count number of triangles ` `# with n total points, out of which m are ` `# collinear. ` `import` `math ` ` ` `# Returns value of binomial coefficient ` `# Code taken from https://goo.gl / vhy4jp ` `def` `nCk(n, k): ` ` ` `C ` `=` `[` `0` `for` `i ` `in` `range` `(` `0` `, k ` `+` `2` `)] ` ` ` ` ` `C[` `0` `] ` `=` `1` `; ` `# nC0 is 1 ` ` ` `for` `i ` `in` `range` `(` `0` `, n ` `+` `1` `): ` ` ` ` ` `# Compute next row of pascal triangle ` ` ` `# using the previous row ` ` ` `for` `j ` `in` `range` `(` `min` `(i, k), ` `0` `, ` `-` `1` `): ` ` ` `C[j] ` `=` `C[j] ` `+` `C[j` `-` `1` `] ` ` ` ` ` `return` `C[k] ` ` ` `# function to calculate number of triangle ` `# can be formed ` `def` `counTriangles(n, m): ` ` ` `return` `(nCk(n, ` `3` `) ` `-` `nCk(m, ` `3` `)) ` ` ` `# driver code ` `n ` `=` `5` `m ` `=` `4` `print` `(counTriangles(n, m)) ` ` ` `# This code is contributed by Gitanjali ` |

*chevron_right*

*filter_none*

## C#

`//C# program to count number of triangles ` `// with n total points, out of which m are ` `// collinear. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns value of binomial coefficient ` ` ` `// Code taken from https://goo.gl/vhy4jp ` ` ` `static` `int` `nCk(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `[] C=` `new` `int` `[k+1]; ` ` ` `for` `(` `int` `i = 0; i <= k; i++) ` ` ` `C[i] = 0; ` ` ` ` ` `// nC0 is 1 ` ` ` `C[0] = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `// Compute next row of pascal triangle ` ` ` `// using the previous row ` ` ` `for` `(` `int` `j = Math.Min(i, k); j > 0; j--) ` ` ` `C[j] = C[j] + C[j - 1]; ` ` ` `} ` ` ` `return` `C[k]; ` ` ` `} ` ` ` ` ` `/* function to calculate number of triangle ` ` ` `can be formed */` ` ` `static` `int` `counTriangles(` `int` `n,` `int` `m) ` ` ` `{ ` ` ` `return` `(nCk(n, 3) - nCk(m, 3)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5, m = 4; ` ` ` `Console.WriteLine(counTriangles(n, m)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of triangles with n total ` `// points, out of which m are collinear. ` ` ` `// Returns value of binomial coefficient ` `// Code taken from https://goo.gl/vhy4jp ` `function` `nCk(` `$n` `, ` `$k` `) ` `{ ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= ` `$k` `; ` `$i` `++) ` ` ` `$C` `[` `$i` `] = 0; ` ` ` ` ` `$C` `[0] = 1; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `// Compute next row of pascal ` ` ` `// triangle using the previous row ` ` ` `for` `(` `$j` `= min(` `$i` `, ` `$k` `); ` `$j` `> 0; ` `$j` `--) ` ` ` `$C` `[` `$j` `] = ` `$C` `[` `$j` `] + ` `$C` `[` `$j` `- 1]; ` ` ` `} ` ` ` `return` `$C` `[` `$k` `]; ` `} ` ` ` `/* function to calculate number ` `of triangles that can be formed */` `function` `counTriangles(` `$n` `, ` `$m` `) ` `{ ` ` ` `return` `(nCk(` `$n` `, 3) - nCk(` `$m` `, 3)); ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `$m` `= 4; ` `echo` `counTriangles(` `$n` `, ` `$m` `); ` `return` `0; ` ` ` `// This code is contributed by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

6

## Recommended Posts:

- Count of different straight lines with total n points with m collinear
- Number of triangles that can be formed with given N points
- Number of triangles formed from a set of points on three lines
- Count the number of possible triangles
- Count number of unique Triangles using STL | Set 1 (Using set)
- Count of sub-sets of size n with total element sum divisible by 3
- Count Integral points inside a Triangle
- Total ways of choosing X men and Y women from a total of M men and W women
- Find all possible triangles with XOR of sides zero
- Number of triangles after N moves
- Program to check similarity of given two triangles
- Area of the circumcircle of any triangles with sides given
- Program to check congruency of two triangles
- Counting Triangles in a Rectangular space using BIT
- Number of Triangles in an Undirected Graph

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.