Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on DP
  • Practice DP
  • MCQs on DP
  • Tutorial on Dynamic Programming
  • Optimal Substructure
  • Overlapping Subproblem
  • Memoization
  • Tabulation
  • Tabulation vs Memoization
  • 0/1 Knapsack
  • Unbounded Knapsack
  • Subset Sum
  • LCS
  • LIS
  • Coin Change
  • Word Break
  • Egg Dropping Puzzle
  • Matrix Chain Multiplication
  • Palindrome Partitioning
  • DP on Arrays
  • DP with Bitmasking
  • Digit DP
  • DP on Trees
  • DP on Graph
Open In App
Next Article:
Number of ways of Triangulation for a Polygon
Next article icon

Number of ways of Triangulation for a Polygon

Last Updated : 06 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

4

The above problem is an application of a catalan numbers. The task is to only find the (n-2)’th Catalan Number. First few catalan numbers for n = 0, 1, 2, 3, 4, 5 are 1 1 2 5 14 42, … (considered from 0th number)

C++
// C++ code of finding Number of ways a convex polygon of n+2  // sides can split into triangles by connecting vertices #include <iostream> using namespace std;  // Function to calculate the binomial coefficient C(n, k) int binomialCoeff(int n, int k) {     // C(n, k) is the same as C(n, n-k)     if (k > n - k) {         k = n - k;     }      int res = 1;     // Calculate the value of n! / (k! * (n-k)!)     for (int i = 0; i < k; ++i) {         res *= (n - i);         res /= (i + 1);     }     return res; }  // Function to find the nth Catalan number int countWays(int n) {     n = n - 2;     // Calculate C(2n, n)     int c = binomialCoeff(2 * n, n);     // Return the nth Catalan number     return c / (n + 1); }  int main() {     int n = 6;     cout << countWays(n) << endl;     return 0; } 
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) int binomialCoeff(int n, int k) {     // C(n, k) is the same as C(n, n-k)     if (k > n - k) {         k = n - k;     }      int res = 1;     // Calculate the value of n! / (k! * (n-k)!)     for (int i = 0; i < k; ++i) {         res *= (n - i);         res /= (i + 1);     }     return res; }  // Function to find the nth Catalan number int countWays(int n) {     n = n - 2;        // Calculate C(2n, n)     int c = binomialCoeff(2 * n, n);        // Return the nth Catalan number     return c / (n + 1); }  int main() {     int n = 6;     printf("%d\n", countWays(n));     return 0; } 
Java
// Java code of finding Number of ways a convex polygon of n+2  // sides can split into triangles by connecting vertices public class PolygonTriangulation {        // Function to calculate the binomial coefficient C(n, k)     static int binomialCoeff(int n, int k) {                // C(n, k) is the same as C(n, n-k)         if (k > n - k) {             k = n - k;         }          int res = 1;                // Calculate the value of n! / (k! * (n-k)!)         for (int i = 0; i < k; ++i) {             res *= (n - i);             res /= (i + 1);         }         return res;     }      // Function to find the nth Catalan number     static int countWays(int n) {         n = n - 2;                // Calculate C(2n, n)         int c = binomialCoeff(2 * n, n);                // Return the nth Catalan number         return c / (n + 1);     }      public static void main(String[] args) {         int n = 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 vertices  def binomialCoeff(n, k):     # C(n, k) is the same as C(n, n-k)     if k > n - k:         k = n - k      res = 1     # Calculate the value of n! / (k! * (n-k)!)     for i in range(k):         res *= (n - i)         res //= (i + 1)     return res  # Function to find the nth Catalan number def countWays(n):     n = n - 2          # Calculate C(2n, n)     c = binomialCoeff(2 * n, n)          # Return the nth Catalan number     return c // (n + 1)  n = 6 print(countWays(n)) 
JavaScript
// JavaScript code of finding Number of ways a convex polygon of n+2  // sides can split into triangles by connecting vertices function binomialCoeff(n, k) {      // C(n, k) is the same as C(n, n-k)     if (k > n - k) {         k = n - k;     }      let res = 1;          // Calculate the value of n! / (k! * (n-k)!)     for (let i = 0; i < k; ++i) {         res *= (n - i);         res /= (i + 1);     }     return res; }  // Function to find the nth Catalan number function countWays(n) {     n = n - 2;          // Calculate C(2n, n)     let c = binomialCoeff(2 * n, n);          // Return the nth Catalan number     return c / (n + 1); }  let n = 6; console.log(countWays(n)); 

Output
14 

Time Complexity: O(n), where n is nth catalan number.
Auxiliary Space: O(1)


Next Article
Number of ways of Triangulation for a Polygon

K

krikti
Improve
Article Tags :
  • Misc
  • Dynamic Programming
  • Mathematical
  • DSA
  • series
  • catalan
Practice Tags :
  • Dynamic Programming
  • Mathematical
  • Misc
  • series

Similar Reads

    Minimum Cost Polygon Triangulation
    A triangulation of a convex polygon is formed by drawing diagonals between non-adjacent vertices (corners) such that the diagonals never intersect. The problem is to find the cost of triangulation with the minimum cost. The cost of a triangulation is sum of the weights of its component triangles. We
    15+ min read
    Count of ways to split N into Triplets forming a Triangle
    Given an integer N, the task is to find the number of ways to split N into ordered triplets which can together form a triangle. Examples: Input: N = 15 Output: Total number of triangles possible are 28 Input: N = 9 Output: Total number of triangles possible is 10 Approach: The following observation
    4 min read
    Number of triangles formed from a set of points on three lines
    Given three integers m, n, and k that store the number of points on lines l1, l2, and l3 respectively that do not intersect. The task is to find the number of triangles that can possibly be formed from these set of points. Examples: Input: m = 3, n = 4, k = 5 Output: 205Input: m = 2, n = 2, k = 1 Ou
    5 min read
    Number of triangles after N moves
    Find the number of triangles in Nth step, Rules: Draw an equilateral triangle at the start. In the i-th move, take the uncolored triangles, divides each of them in 4 parts of equal areas and color the central part. Keep a count of triangles till the Nth step. Examples: Input : 1 Output : 5 Explanati
    6 min read
    Number of triangles that can be formed with given N points
    Given X and Y coordinates of N points on a Cartesian plane. The task is to find the number of possible triangles with the non-zero area that can be formed by joining each point to every other point. Examples: Input: P[] = {{0, 0}, {2, 0}, {1, 1}, {2, 2}} Output: 3 Possible triangles can be [(0, 0},
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences