Count ways to divide circle using N non-intersecting chord | Set-2 - GeeksforGeeks

07 Dec, 2021
Given a number N. The task is to find the number of ways you can draw N chords in a circle with 2*N points such that no two chords intersect. Two ways are different if there exists a chord that is present in one way and not in other. As the answer could be large print it modulo 10^9+7.
Examples: 
Input : N = 2 Output : 2 If points are numbered 1 to 4 in clockwise direction, then different ways to draw chords are: {(1-2), (3-4)} and {(1-4), (2-3)}
Input :N = 1 Output : 1 
Approach: If we draw a chord between any two points, the current set of points gets broken into two smaller sets S_1 and S_2. If we draw a chord from a point in S_1 to a point in S_2, it will surely intersect the chord we’ve just drawn. So, we can arrive at a recurrence that:
Ways(n) = sum[i = 0 to n-1] { Ways(i)*Ways(n-i-1) }.  
The above recurrence relation is similar to the recurrence relation for nth Catalan number which is equal to 2nCn / (n+1). Instead of dividing the numeration with the denomination, multiply the numerator with the modulo inverse of the denominator as division is not allowed in the modulo domain.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Function to calculate x^y %mod efficientlyint power(long long x, int y, int mod){     // Initialize the answer    long long res = 1;    while (y) {         // If power is odd        if (y & 1)             // Update the answer            res = (res * x) % mod;         // Square the base and half the exponent        x = (x * x) % mod;        y = (y >> 1);    }     // Return the value    return (int)(res % mod);}   // Function to calculate ncr%mod efficientlyint ncr(int n, int r, int mod){     // Initialize the answer    long long res = 1;     // Calculate ncr in O(r)    for (int i = 1; i <= r; i += 1) {         // Multiply with the numerator factor        res = (res * (n - i + 1)) % mod;         // Calculate the inverse of factor of denominator        int inv = power(i, mod - 2, mod);         // Multiply with inverse value        res = (res * inv) % mod;    }     // Return answer value    return (int)(res%mod);} // Function to return the number// of non intersecting chordsint NoOfChords(int A){     // define mod value    int mod = 1e9 + 7;     // Value of C(2n, n)    long long ans = ncr(2 * A, A, mod);     // Modulo inverse of (n+1)    int inv = power(A + 1, mod - 2, mod);     // Multiply with modulo inverse    ans = (ans * inv) % mod;     // Return the answer    return (int)(ans%mod);} // Driver codeint main(){     int N = 2;         // Function call    cout << NoOfChords(N);     return 0;}
// Java implementation of the approachclass GFG{     // Function to calculate x^y %mod efficiently    static int power(long x, int y, int mod)    {             // Initialize the answer        long res = 1;        while (y != 0)        {                 // If power is odd            if ((y & 1) == 1)                     // Update the answer                res = (res * x) % mod;                 // Square the base and half the exponent            x = (x * x) % mod;            y = (y >> 1);        }             // Return the value        return (int)(res % mod);    }         // Function to calculate ncr%mod efficiently    static int ncr(int n, int r, int mod)    {             // Initialize the answer        long res = 1;             // Calculate ncr in O(r)        for (int i = 1; i <= r; i += 1)        {                 // Multiply with the numerator factor            res = (res * (n - i + 1)) % mod;                 // Calculate the inverse of            // factor of denominator            int inv = power(i, mod - 2, mod);                 // Multiply with inverse value            res = (res * inv) % mod;        }             // Return answer value        return (int)(res % mod);    }         // Function to return the number    // of non intersecting chords    static int NoOfChords(int A)    {             // define mod value        int mod = (int)(1e9 + 7);             // Value of C(2n, n)        long ans = ncr(2 * A, A, mod);             // Modulo inverse of (n+1)        int inv = power(A + 1, mod - 2, mod);             // Multiply with modulo inverse        ans = (ans * inv) % mod;             // Return the answer        return (int)(ans % mod);    }         // Driver code    public static void main(String[] args)    {        int N = 2;             // Function call        System.out.println(NoOfChords(N));    }} // This code is contributed by 29AjayKumar
# Python3 implementation of the above approach # Function to calculate x^y %mod efficientlydef power(x, y, mod):     # Initialize the answer    res = 1    while (y):         # If power is odd        if (y & 1):             # Update the answer            res = (res * x) % mod         # Square the base and half the exponent        x = (x * x) % mod        y = (y >> 1)      # Return the value    return (res % mod) # Function to calculate ncr%mod efficientlydef ncr(n, r, mod):      # Initialize the answer    res = 1     # Calculate ncr in O(r)    for i in range(1,r+1):         # Multiply with the numerator factor        res = (res * (n - i + 1)) % mod         # Calculate the inverse of factor of denominator        inv = power(i, mod - 2, mod)         # Multiply with inverse value        res = (res * inv) % mod      # Return answer value    return (res%mod) # Function to return the number# of non intersecting chordsdef NoOfChords(A):      # define mod value    mod = 10**9 + 7     # Value of C(2n, n)    ans = ncr(2 * A, A, mod)     # Modulo inverse of (n+1)    inv = power(A + 1, mod - 2, mod)     # Multiply with modulo inverse    ans = (ans * inv) % mod     # Return the answer    return (ans%mod)  # Driver code N = 2 # Function callprint(NoOfChords(N)) # This code is contributed by mohit kumar 29
// Java implementation of the above approachusing System; class GFG{     // Function to calculate x^y %mod efficiently    static int power(long x, int y, int mod)    {             // Initialize the answer        long res = 1;        while (y != 0)        {                 // If power is odd            if ((y & 1) == 1)                     // Update the answer                res = (res * x) % mod;                 // Square the base and half the exponent            x = (x * x) % mod;            y = (y >> 1);        }             // Return the value        return (int)(res % mod);    }         // Function to calculate ncr%mod efficiently    static int ncr(int n, int r, int mod)    {             // Initialize the answer        long res = 1;             // Calculate ncr in O(r)        for (int i = 1; i <= r; i += 1)        {                 // Multiply with the numerator factor            res = (res * (n - i + 1)) % mod;                 // Calculate the inverse of factor of denominator            int inv = power(i, mod - 2, mod);                 // Multiply with inverse value            res = (res * inv) % mod;        }             // Return answer value        return (int)(res % mod);    }         // Function to return the number    // of non intersecting chords    static int NoOfChords(int A)    {             // define mod value        int mod = (int)(1e9 + 7);             // Value of C(2n, n)        long ans = ncr(2 * A, A, mod);             // Modulo inverse of (n+1)        int inv = power(A + 1, mod - 2, mod);             // Multiply with modulo inverse        ans = (ans * inv) % mod;             // Return the answer        return (int)(ans % mod);    }         // Driver code    public static void Main ()    {        int N = 2;                 // Function call        Console.WriteLine(NoOfChords(N));    }} // This code is contributed by AnkitRai01
<script> // JavaScript implementation of the approach  // Function to calculate x^y %mod efficientlyfunction power(x , y , mod){     // Initialize the answer    var res = 1;    while (y != 0)    {         // If power is odd        if ((y & 1) == 1)             // Update the answer            res = (res * x) % mod;         // Square the base and half the exponent        x = (x * x) % mod;        y = (y >> 1);    }     // Return the value    return parseInt(res % mod);} // Function to calculate ncr%mod efficientlyfunction ncr(n , r , mod){     // Initialize the answer    var res = 1;     // Calculate ncr in O(r)    for (var i = 1; i <= r; i += 1)    {         // Multiply with the numerator factor        res = (res * (n - i + 1)) % mod;         // Calculate the inverse of        // factor of denominator        var inv = power(i, mod - 2, mod);         // Multiply with inverse value        res = (res * inv) % mod;    }     // Return answer value    return parseInt(res % mod);} // Function to return the number// of non intersecting chordsfunction NoOfChords( A){     // define mod value    var mod = parseInt(7);     // Value of C(2n, n)    var ans = ncr(2 * A, A, mod);     // Modulo inverse of (n+1)    var inv = power(A + 1, mod - 2, mod);     // Multiply with modulo inverse    ans = (ans * inv) % mod;     // Return the answer    return parseInt(ans % mod);} // Driver codevar N = 2; // Function calldocument.write(NoOfChords(N));  // This code is contributed by 29AjayKumar </script>
2
 
Time complexity : O(N*log(mod)) 
mohit kumar 29
ankthon
29AjayKumar
simmytarika5
catalan
Combinatorial
Greedy
Mathematical
Greedy
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Print all subsets of given size of a set
Python program to get all subsets of given size of a set
Make all combinations of size k
Find all distinct subsets of a given set using BitMasking Approach
Count Derangements (Permutation such that no element appears in its original position)
Dijkstra's shortest path algorithm | Greedy Algo-7
Program for array rotation
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Huffman Coding | Greedy Algo-3