Count of different straight lines with total n points with m collinear
Last Updated :
15 Dec, 2022
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 = nC2 – mC2 + 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 nC2 ways.
Number of straight line formed by 10 points when no 2 of them are co-linear = 10C2…..…(i)
Similarly, the number of straight lines formed by 4 points when no 2 of them are co-linear = 4C2….(ii)
Since straight lines formed by these 4 points are same, straight lines formed by them will reduce to only one.
Required number of straight lines formed = 10C2– 4C2 + 1 = 45 – 6 + 1 = 40
Implementation of the approach is given as:
C++
#include <bits/stdc++.h>
using namespace std;
int nCk( int n, int k)
{
int C[k+1];
memset (C, 0, sizeof (C));
C[0] = 1;
for ( int i = 1; i <= n; i++)
{
for ( int j = min(i, k); j > 0; j--)
C[j] = C[j] + C[j-1];
}
return C[k];
}
int count_Straightlines( int n, int m)
{
return (nCk(n, 2) - nCk(m, 2)+1);
}
int main()
{
int n = 4, m = 3 ;
cout << count_Straightlines(n, m);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static int nCk( int n, int k)
{
int [] C = new int [k + 1 ];
C[ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = Math.min(i, k); j > 0 ; j--)
C[j] = C[j] + C[j - 1 ];
}
return C[k];
}
public static int count_Straightlines( int n, int m)
{
return (nCk(n, 2 ) - nCk(m, 2 ) + 1 );
}
public static void main(String argc[])
{
int n = 4 , m = 3 ;
System.out.println(count_Straightlines(n, m));
}
}
|
Python
def nCk(n, k):
C = [ 0 ] * (k + 1 )
C[ 0 ] = 1
for i in range ( 1 , n + 1 ):
j = min (i, k)
while (j> 0 ):
C[j] = C[j] + C[j - 1 ]
j = j - 1
return C[k]
def count_Straightlines(n, m):
return (nCk(n, 2 ) - nCk(m, 2 ) + 1 )
n = 4
m = 3
print ( count_Straightlines(n, m) );
|
C#
using System;
public class GfG {
public static int nCk( int n, int k)
{
int [] C = new int [k + 1];
C[0] = 1;
for ( int i = 1; i <= n; i++)
{
for ( int j = Math.Min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}
public static int count_Straightlines( int n, int m)
{
return (nCk(n, 2) - nCk(m, 2) + 1);
}
public static void Main(String []args)
{
int n = 4, m = 3;
Console.WriteLine(count_Straightlines(n, m));
}
}
|
PHP
<?php
function nCk( $n , $k )
{
$C = array_fill (0, $k + 1, NULL);
$C [0] = 1;
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = min( $i , $k ); $j > 0; $j --)
$C [ $j ] = $C [ $j ] + $C [ $j -1];
}
return $C [ $k ];
}
function count_Straightlines( $n , $m )
{
return (nCk( $n , 2) - nCk( $m , 2) + 1);
}
$n = 4;
$m = 3;
echo (count_Straightlines( $n , $m ));
?>
|
Javascript
<script>
function nCk(n, k)
{
let C = new Array(k+1);
C.fill(0);
C[0] = 1;
for (let i = 1; i <= n; i++)
{
for (let j = Math.min(i, k); j > 0; j--)
C[j] = C[j] + C[j-1];
}
return C[k];
}
function count_Straightlines(n,m)
{
return (nCk(n, 2) - nCk(m, 2)+1);
}
let n = 4, m = 3 ;
document.write(count_Straightlines(n, m));
</script>
|
Output:
4
Time Complexity: O(max(n,m))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...