Given a convex polygon with n sides. The task is to calculate the number of ways in which triangles can be formed by connecting vertices with non-crossing line segments. Examples:
Input: n = 3 Output: 1 It is already a triangle so it can only be formed in 1 way.
Input: n = 4 Output: 2 It can be cut into 2 triangles by using either pair of opposite vertices.
Input: n = 6 Output: 14 (Below, There are all 14 polygon). It can be cut into 2 triangles by using either pair of opposite vertices.
// C++ code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting vertices#include<iostream>usingnamespacestd;// Function to calculate the binomial coefficient C(n, k)intbinomialCoeff(intn,intk){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}intres=1;// Calculate the value of n! / (k! * (n-k)!)for(inti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberintcountWays(intn){n=n-2;// Calculate C(2n, n)intc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}intmain(){intn=6;cout<<countWays(n)<<endl;return0;}
C
// C code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting vertices#include<stdio.h>// Function to calculate the binomial coefficient C(n, k)intbinomialCoeff(intn,intk){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}intres=1;// Calculate the value of n! / (k! * (n-k)!)for(inti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberintcountWays(intn){n=n-2;// Calculate C(2n, n)intc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}intmain(){intn=6;printf("%d\n",countWays(n));return0;}
Java
// Java code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting verticespublicclassPolygonTriangulation{// Function to calculate the binomial coefficient C(n, k)staticintbinomialCoeff(intn,intk){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}intres=1;// Calculate the value of n! / (k! * (n-k)!)for(inti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberstaticintcountWays(intn){n=n-2;// Calculate C(2n, n)intc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}publicstaticvoidmain(String[]args){intn=6;System.out.println(countWays(n));}}
Python
# Python code of finding Number of ways a convex polygon of n+2 # sides can split into triangles by connecting verticesdefbinomialCoeff(n,k):# C(n, k) is the same as C(n, n-k)ifk>n-k:k=n-kres=1# Calculate the value of n! / (k! * (n-k)!)foriinrange(k):res*=(n-i)res//=(i+1)returnres# Function to find the nth Catalan numberdefcountWays(n):n=n-2# Calculate C(2n, n)c=binomialCoeff(2*n,n)# Return the nth Catalan numberreturnc//(n+1)n=6print(countWays(n))
JavaScript
// JavaScript code of finding Number of ways a convex polygon of n+2 // sides can split into triangles by connecting verticesfunctionbinomialCoeff(n,k){// C(n, k) is the same as C(n, n-k)if(k>n-k){k=n-k;}letres=1;// Calculate the value of n! / (k! * (n-k)!)for(leti=0;i<k;++i){res*=(n-i);res/=(i+1);}returnres;}// Function to find the nth Catalan numberfunctioncountWays(n){n=n-2;// Calculate C(2n, n)letc=binomialCoeff(2*n,n);// Return the nth Catalan numberreturnc/(n+1);}letn=6;console.log(countWays(n));
Output
14
Time Complexity: O(n), where n is nth catalan number. Auxiliary Space: O(1)