Count of triangles with total n points with m collinear
Last Updated :
20 May, 2022
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 = nC3 – mC3
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 nC3 ways.
Number of triangles formed by 10 points when no 3 of them are co-linear = 10C3……(i)
Similarly, the number of triangles formed by 4 points when no 3 of them are co-linear = 4C3……..(ii)
Since triangle formed by these 4 points are not valid, required number of triangles formed = 10C3 – 4C3 = 120 – 4 = 116
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 countTriangles( int n, int m)
{
return (nCk(n, 3) - nCk(m, 3));
}
int main()
{
int n = 5, m = 4;
cout << countTriangles(n, m);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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 ;
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];
}
static int countTriangles( 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(countTriangles(n, m));
}
}
|
Python3
import math
def nCk(n, k):
C = [ 0 for i in range ( 0 , k + 2 )]
C[ 0 ] = 1 ;
for i in range ( 0 , n + 1 ):
for j in range ( min (i, k), 0 , - 1 ):
C[j] = C[j] + C[j - 1 ]
return C[k]
def countTriangles(n, m):
return (nCk(n, 3 ) - nCk(m, 3 ))
n = 5
m = 4
print (countTriangles(n, m))
|
C#
using System;
class GFG {
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;
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];
}
static int countTriangles( int n, int m)
{
return (nCk(n, 3) - nCk(m, 3));
}
public static void Main ()
{
int n = 5, m = 4;
Console.WriteLine(countTriangles(n, m));
}
}
|
PHP
<?php
function nCk( $n , $k )
{
for ( $i = 0; $i <= $k ; $i ++)
$C [ $i ] = 0;
$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 countTriangles( $n , $m )
{
return (nCk( $n , 3) - nCk( $m , 3));
}
$n = 5;
$m = 4;
echo countTriangles( $n , $m );
return 0;
?>
|
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 countTriangles(n, m)
{
return (nCk(n, 3) - nCk(m, 3));
}
let n = 5, m = 4;
document.write(countTriangles(n, m));
</script>
|
Output :
6
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...