 GeeksforGeeks App
Open App Browser
Continue

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

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 :
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 :

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++

 `// C++ implementation of the above approach``#include ``using` `namespace` `std;` `// Function to calculate x^y %mod efficiently``int` `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 efficiently``int` `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 chords``int` `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 code``int` `main()``{` `    ``int` `N = 2;``    ` `    ``// Function call``    ``cout << NoOfChords(N);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``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(String[] args)``    ``{``        ``int` `N = ``2``;``    ` `        ``// Function call``        ``System.out.println(NoOfChords(N));``    ``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the above approach` `# Function to calculate x^y %mod efficiently``def` `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 efficiently``def` `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 chords``def` `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 call``print``(NoOfChords(N))` `# This code is contributed by mohit kumar 29`

## C#

 `// Java implementation of the above approach``using` `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`

## Javascript

 ``

Output:

`2`

Time complexity : O(N*log(mod))

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up