Count ways to divide circle using N non-intersecting chord | Set-2
Last Updated :
06 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int power( long long x, int y, int mod)
{
long long res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
x = (x * x) % mod;
y = (y >> 1);
}
return ( int )(res % mod);
}
int ncr( int n, int r, int mod)
{
long long res = 1;
for ( int i = 1; i <= r; i += 1) {
res = (res * (n - i + 1)) % mod;
int inv = power(i, mod - 2, mod);
res = (res * inv) % mod;
}
return ( int )(res%mod);
}
int NoOfChords( int A)
{
int mod = 1e9 + 7;
long long ans = ncr(2 * A, A, mod);
int inv = power(A + 1, mod - 2, mod);
ans = (ans * inv) % mod;
return ( int )(ans%mod);
}
int main()
{
int N = 2;
cout << NoOfChords(N);
return 0;
}
|
Java
class GFG
{
static int power( long x, int y, int mod)
{
long res = 1 ;
while (y != 0 )
{
if ((y & 1 ) == 1 )
res = (res * x) % mod;
x = (x * x) % mod;
y = (y >> 1 );
}
return ( int )(res % mod);
}
static int ncr( int n, int r, int mod)
{
long res = 1 ;
for ( int i = 1 ; i <= r; i += 1 )
{
res = (res * (n - i + 1 )) % mod;
int inv = power(i, mod - 2 , mod);
res = (res * inv) % mod;
}
return ( int )(res % mod);
}
static int NoOfChords( int A)
{
int mod = ( int )(1e9 + 7 );
long ans = ncr( 2 * A, A, mod);
int inv = power(A + 1 , mod - 2 , mod);
ans = (ans * inv) % mod;
return ( int )(ans % mod);
}
public static void main(String[] args)
{
int N = 2 ;
System.out.println(NoOfChords(N));
}
}
|
Python3
def power(x, y, mod):
res = 1
while (y):
if (y & 1 ):
res = (res * x) % mod
x = (x * x) % mod
y = (y >> 1 )
return (res % mod)
def ncr(n, r, mod):
res = 1
for i in range ( 1 ,r + 1 ):
res = (res * (n - i + 1 )) % mod
inv = power(i, mod - 2 , mod)
res = (res * inv) % mod
return (res % mod)
def NoOfChords(A):
mod = 10 * * 9 + 7
ans = ncr( 2 * A, A, mod)
inv = power(A + 1 , mod - 2 , mod)
ans = (ans * inv) % mod
return (ans % mod)
N = 2
print (NoOfChords(N))
|
C#
using System;
class GFG
{
static int power( long x, int y, int mod)
{
long res = 1;
while (y != 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
x = (x * x) % mod;
y = (y >> 1);
}
return ( int )(res % mod);
}
static int ncr( int n, int r, int mod)
{
long res = 1;
for ( int i = 1; i <= r; i += 1)
{
res = (res * (n - i + 1)) % mod;
int inv = power(i, mod - 2, mod);
res = (res * inv) % mod;
}
return ( int )(res % mod);
}
static int NoOfChords( int A)
{
int mod = ( int )(1e9 + 7);
long ans = ncr(2 * A, A, mod);
int inv = power(A + 1, mod - 2, mod);
ans = (ans * inv) % mod;
return ( int )(ans % mod);
}
public static void Main ()
{
int N = 2;
Console.WriteLine(NoOfChords(N));
}
}
|
Javascript
<script>
function power(x , y , mod)
{
var res = 1;
while (y != 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
x = (x * x) % mod;
y = (y >> 1);
}
return parseInt(res % mod);
}
function ncr(n , r , mod)
{
var res = 1;
for ( var i = 1; i <= r; i += 1)
{
res = (res * (n - i + 1)) % mod;
var inv = power(i, mod - 2, mod);
res = (res * inv) % mod;
}
return parseInt(res % mod);
}
function NoOfChords( A)
{
var mod = parseInt(7);
var ans = ncr(2 * A, A, mod);
var inv = power(A + 1, mod - 2, mod);
ans = (ans * inv) % mod;
return parseInt(ans % mod);
}
var N = 2;
document.write(NoOfChords(N));
</script>
|
Time complexity : O(N*log(mod))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...