Number of triangles formed by joining vertices of n-sided polygon with one side common - GeeksforGeeks

09 Dec, 2021
Given N-sided polygon we need to find the number of triangles formed by joining the vertices of the given polygon with exactly one side being common.Examples: 
Input : 6 Output : 12 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The two triangles formed has one side (AB) common with that of a polygon.It depicts that with one edge of a hexagon we can make two triangles with one side common. We can’t take C or F as our third vertex as it will make 2 sides common with the hexagon.  
Triangle with one side common of a hexagon
No of triangles formed ie equal to 12 as there are 6 edges in a hexagon.Input : 5 Output : 5  
 
Approach :  
To make a triangle with one side common with a polygon the two vertices adjacent to the chosen common vertices cannot be considered as the third vertex of a triangle.
First select any one edge from the polygon. Consider this edge to be the common edge. Number of ways to select an edge in a polygon would be equal to n.
Now ,to form a triangle ,select any of the (n-4) vertices left .Two vertices of the common edge and two vertices adjacent to the common edge cannot be considered.
Number of triangle formed by joining the vertices of an n-sided polygon with one side common would be equal to n * ( n – 4) .
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to implement// the above problem#include <bits/stdc++.h>using namespace std; // Function to find the number of trianglesvoid findTriangles(int n){    int num;    num = n * (n - 4);     // print the number of triangles    cout << num;} // Driver codeint main(){    // initialize the number of sides of a polygon    int n;    n = 6;     findTriangles(n);     return 0;}
// Java program to implement// the above problemclass GFG{     // Function to find the number of trianglesstatic void findTriangles(int n){    int num;    num = n * (n - 4);     // print the number of triangles    System.out.println(num);} // Driver codepublic static void main(String [] args){    // initialize the number of sides of a polygon    int n;    n = 6;     findTriangles(n); }} // This code is contributed by ihritik
# Python3 program to implement# the above problem # Function to find the number of trianglesdef findTriangles(n):    num = n * (n - 4)     # print the number of triangles    print(num) # Driver code# initialize the number of sides of a polygonn = 6 findTriangles(n) # This code is contributed by mohit kumar 29
// C# program to implement// the above problemusing System; class GFG{     // Function to find the number of trianglesstatic void findTriangles(int n){    int num;    num = n * (n - 4);     // print the number of triangles    Console.WriteLine(num);} // Driver codepublic static void Main(){    // initialize the number of sides of a polygon    int n;    n = 6;     findTriangles(n); }} // This code is contributed by ihritik
<script> // JavaScript program to implement// the above problem      // Function to find the number of trianglesfunction findTriangles(n){    var num;    num = n * (n - 4);     // print the number of triangles    document.write(num);} // Driver code // initialize the number of sides of a polygonvar n;n = 6; findTriangles(n);  // This code contributed by shikhasingrajput </script>
12
 
Time Complexity: O(1) 
mohit kumar 29
ihritik
nidhi_biet
shikhasingrajput
surindertarika1234
C++ Programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C++ Program for QuickSort
cin in C++
delete keyword in C++
Shallow Copy and Deep Copy in C++
Check if given number is perfect square
Circle and Lattice Points
Closest Pair of Points using Divide and Conquer algorithm
How to check if two given line segments intersect?
Program for distance between two points on earth
How to check if a given point lies inside or outside a polygon?