Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
Given a convex polygon with n+2 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 = 1
Output: 1
It is already a triangle so it can only be formed in 1 way.Input: n = 2
Output: 2
It can be cut into 2 triangles by using either pair of opposite vertices.

The above problem is an application of a catalan numbers. So, the task is to only find the n’th Catalan Number. First few catalan numbers are 1 1 2 5 14 42 132 429 1430 4862, … (considered from 0th number)
Below is the program to find Nth catalan number:
C++
// C++ program to find the // nth catalan number #include <bits/stdc++.h> using namespace std; // Returns value of Binomial Coefficient C(n, k) unsigned long int binomialCoeff(unsigned int n, unsigned int k) { unsigned long int res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // A Binomial coefficient based function // to find nth catalan // number in O(n) time unsigned long int catalan(unsigned int n) { // Calculate value of 2nCn unsigned long int c = binomialCoeff(2 * n, n); // return 2nCn/(n+1) return c / (n + 1); } // Driver code int main() { int n = 3; cout << catalan(n) << endl; return 0; } |
Java
// Java program to find the // nth catalan number class GFG { // Returns value of Binomial // Coefficient C(n, k) static long binomialCoeff(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / // [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // A Binomial coefficient // based function to find // nth catalan number in // O(n) time static long catalan( int n) { // Calculate value of 2nCn long c = binomialCoeff(2 * n, n); // return 2nCn/(n+1) return c / (n + 1); } // Driver code public static void main(String[] args) { int n = 3; System.out.println(catalan(n)); } } // This code is contributed // by Arnab Kundu |
Python3
# Python3 program to find the
# nth catalan number
# Returns value of Binomial
# Coefficient C(n, k)
def binomialCoeff(n, k):
res = 1;
# Since C(n, k) = C(n, n-k)
if (k > n – k):
k = n – k;
# Calculate value of
# [n*(n-1)*—*(n-k+1)] /
# [k*(k-1)*—*1]
for i in range(k):
res *= (n – i);
res /= (i + 1);
return res;
# A Binomial coefficient based
# function to find nth catalan
# number in O(n) time
def catalan(n):
# Calculate value of 2nCn
c = binomialCoeff(2 * n, n);
# return 2nCn/(n+1)
return int(c / (n + 1));
# Driver code
n = 3;
print(catalan(n));
# This code is contributed
# by mits
C#
// C# program to find the // nth catalan number using System; class GFG { // Returns value of Binomial // Coefficient C(n, k) static long binomialCoeff(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / // [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // A Binomial coefficient // based function to find // nth catalan number in // O(n) time static long catalan( int n) { // Calculate value of 2nCn long c = binomialCoeff(2 * n, n); // return 2nCn/(n+1) return c / (n + 1); } // Driver code public static void Main() { int n = 3; Console.WriteLine(catalan(n)); } } // This code is contributed // by Subhadeep |
PHP
<?php // PHP program to find the // nth catalan number // Returns value of Binomial // Coefficient C(n, k) function binomialCoeff($n, $k) { $res = 1; // Since C(n, k) = C(n, n-k) if ($k > $n - $k) $k = $n - $k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / // [k*(k-1)*---*1] for ($i = 0; $i < $k; ++$i) { $res *= ($n - $i); $res /= ($i + 1); } return $res; } // A Binomial coefficient based // function to find nth catalan // number in O(n) time function catalan($n) { // Calculate value of 2nCn $c = binomialCoeff(2 * $n, $n); // return 2nCn/(n+1) return $c / ($n + 1); } // Driver code $n = 3; echo catalan($n); // This code is contributed // by chandan_jnu. ?> |
5
Recommended Posts:
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Check if it is possible to create a polygon with given n sides
- Count the number of possible triangles
- Number of triangles after N moves
- Number of trees whose sum of degrees of all the vertices is L
- Number of triangles that can be formed with given N points
- Count number of right triangles possible with a given perimeter
- Number of Simple Graph with N Vertices and M Edges
- Number of triangles in a plane if no more than two points are collinear
- Number of possible Triangles in a Cartesian coordinate system
- Number of triangles possible with given lengths of sticks which are powers of 2
- Number of triangles formed from a set of points on three lines
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



