There are ‘n’ points in a plane out of which ‘m points are collinear. How many different straight lines can form?

Examples:

Input : n = 3, m = 3 Output : 1 We can form only 1 distinct straight line using 3 collinear points Input : n = 10, m = 4 Output : 40

Number of distinct Straight lines =

^{n}C_{2}–^{m}C_{2}+ 1

How does this formula work?

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

Number of straight line formed by 10 points when no 2 of them are co-linear =

…..…(i)^{10}C_{2}

Similarly, the number of straight lines formed by 4 points when no 2 of them are co-linear =….(ii)^{4}C_{2}Since straight lines formed by these 4 points are sane, straight lines formed by them will reduce to only one.

Required number of straight lines formed =^{10}C_{2}–^{4}C_{2}+ 1 = 45 – 6 + 1 = 40

Implementation of the approach is given as:

## C++

`// CPP program to count number of straight lines ` `// 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 straight lines ` ` ` `can be formed */` `int` `count_Straightlines(` `int` `n,` `int` `m) ` `{ ` ` ` ` ` `return` `(nCk(n, 2) - nCk(m, 2)+1); ` ` ` `} ` ` ` ` ` `/* driver function*/` `int` `main() ` `{ ` ` ` ` ` `int` `n = 4, m = 3 ; ` ` ` `cout << count_Straightlines(n, m); ` ` ` `return` `0; ` ` ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of straight lines ` `// with n total points, out of which m are ` `// collinear. ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// Returns value of binomial coefficient ` ` ` `// Code taken from https://goo.gl/vhy4jp ` ` ` `public` `static` `int` `nCk(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `[] C = ` `new` `int` `[k + ` `1` `]; ` ` ` ` ` `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 straight lines ` ` ` `can be formed */` ` ` `public` `static` `int` `count_Straightlines(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `(nCk(n, ` `2` `) - nCk(m, ` `2` `) + ` `1` `); ` ` ` `} ` ` ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main(String argc[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `, m = ` `3` `; ` ` ` `System.out.println(count_Straightlines(n, m)); ` ` ` `} ` ` ` ` ` `// This code is contributed by Sagar Shukla ` `} ` |

*chevron_right*

*filter_none*

## Python

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

*chevron_right*

*filter_none*

## C#

`// C# program to count number of straight ` `// lines with n total points, out of ` `// which m are collinear. ` `using` `System; ` ` ` `public` `class` `GfG { ` ` ` ` ` `// Returns value of binomial coefficient ` ` ` `// Code taken from https://goo.gl/vhy4jp ` ` ` `public` `static` `int` `nCk(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `[] C = ` `new` `int` `[k + 1]; ` ` ` ` ` `// 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 ` ` ` `// straight lines can be formed ` ` ` `public` `static` `int` `count_Straightlines(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `return` `(nCk(n, 2) - nCk(m, 2) + 1); ` ` ` `} ` ` ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `n = 4, m = 3; ` ` ` `Console.WriteLine(count_Straightlines(n, m)); ` ` ` `} ` ` ` ` ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number of straight lines ` `// with n total points, out of which m are ` `// collinear. ` ` ` `// Returns value of binomial coefficient ` `function` `nCk(` `$n` `, ` `$k` `) ` `{ ` ` ` `$C` `= ` `array_fill` `(0, ` `$k` `+ 1, NULL); ` ` ` ` ` `$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 straight lines ` `// can be formed ` `function` `count_Straightlines(` `$n` `, ` `$m` `) ` `{ ` ` ` ` ` `return` `(nCk(` `$n` `, 2) - nCk(` `$m` `, 2) + 1); ` ` ` `} ` ` ` `// Driver Code ` `$n` `= 4; ` `$m` `= 3; ` `echo` `(count_Straightlines(` `$n` `, ` `$m` `)); ` ` ` `// This code is contributed ` `// by Prasad Kshirsagar ` `?> ` |

*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
- Maximum points of intersections possible among X circles and Y straight lines
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Program to check if three points are collinear
- Number of triangles in a plane if no more than two points are collinear
- Check whether two straight lines are orthogonal or not
- Check if three straight lines are concurrent or not
- Puzzle | Connect 9 circles each arranged at center of a Matrix using 3 straight lines
- Represent a given set of points by the best possible straight line
- Check whether a straight line can be formed using N co-ordinate points
- Minimum lines to cover all points
- Maximum points of intersection n lines
- Non-crossing lines to connect points in a circle
- Number of triangles formed from a set of points on three lines
- Find whether only two parallel lines contain all coordinates points or not
- Number of lines from given N points not parallel to X or Y axis
- Number of pairs of lines having integer intersection points
- Total number of triangles formed when there are H horizontal and V vertical lines
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A

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.