    # Out of 8 given points if 5 points are collinear then the number of triangles that can be formed is

View Discussion

Improve Article

Save Article

• Discuss
• View Discussion

Improve Article

Save Article

Given n points in a plane and no more than two points are collinear, the task is to count the number of triangles in a given plane.

Examples:

Input : n = 3 Output : 1 Input : n = 4 Output : 4 Let there are n points in a plane and no three or more points are collinear then number of triangles in the given plane is given by ## C++

#include <bits/stdc++.h>

using namespace std;

int countNumberOfTriangles(int n)

{

return n * (n - 1) * (n - 2) / 6;

}

int main()

{

int n = 4;

cout << countNumberOfTriangles(n);

return 0;

}

## C

#include <stdio.h>

int countNumberOfTriangles(int n)

{

return n * (n - 1) * (n - 2) / 6;

}

int main()

{

int n = 4;

printf("%d",countNumberOfTriangles(n));

return 0;

}

## Java

import java.io.*;

class GFG {

static int countNumberOfTriangles(int n)

{

return n * (n - 1) * (n - 2) / 6;

}

public static void main(String[] args)

{

int n = 4;

System.out.println(

countNumberOfTriangles(n));

}

}

## Python3

def countNumberOfTriangles(n) :

return (n * (n - 1) *

(n - 2) // 6)

if __name__ == '__main__' :

n = 4

print(countNumberOfTriangles(n))

## C#

using System;

class GFG

{

static int countNumberOfTriangles(int n)

{

return n * (n - 1) *

(n - 2) / 6;

}

public static void Main()

{

int n = 4;

Console.WriteLine(

countNumberOfTriangles(n));

}

}

## PHP

<?php

function countNumberOfTriangles(\$n)

{

return \$n * (\$n - 1) *

(\$n - 2) / 6;

}

\$n = 4;

echo countNumberOfTriangles(\$n);

?>

## Javascript

<script>

function countNumberOfTriangles(n)

{

return n * (n - 1) * (n - 2) / 6;

}

var n = 4;

document.write(countNumberOfTriangles(n));

</script>

Time complexity: O(1)

Auxiliary space: O(1)

1) 8C3

2) 8C3 – 5C3

3) 8C3 – 5C3 – 1

4) None of these

Answer: (3) 8C3 – 5C3 – 1

Solution: We have total of 8 points here. The number of points we need to make a triangle is 3.

We can select 3 non-collinear points out of 8 points in 8C3 ways

We can deduct the collinear points in 5C3 ways

Therefore, the number of triangles formed out of 8 points removing the possibility of selecting collinear points = 8C3 – 5C3 – 1

### How many triangles can be formed from a set of 8 points where 5 of these points are collinear?

Now, total possible Triangle that can be formed choosing any 3 points without any colinear constraint is 8C3 = 56.

### How many triangles can be made with 8 points?

The number of triangles that can be drawn by joining these points = 8C3 = 56.

### How many triangles can form from collinear points?

Hence required number of triangles = nC3−mC3.

### How many triangles can be formed from 5 points?

Answer: There are 10 triangles that can be obtained from 5 points in a plane. 